home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cp / class.c < prev    next >
C/C++ Source or Header  |  1994-07-09  |  153KB  |  4,941 lines

  1. /* Functions related to building classes and their related objects.
  2.    Copyright (C) 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include <stdio.h>
  27. #include "cp-tree.h"
  28. #include "flags.h"
  29.  
  30. #include "obstack.h"
  31. #define obstack_chunk_alloc xmalloc
  32. #define obstack_chunk_free free
  33.  
  34. extern struct obstack permanent_obstack;
  35.  
  36. /* This is how we tell when two virtual member functions are really the
  37.    same. */
  38. #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
  39.  
  40. extern void set_class_shadows PROTO ((tree));
  41.  
  42. /* Way of stacking class types.  */
  43. static tree *current_class_base, *current_class_stack;
  44. static int current_class_stacksize;
  45. int current_class_depth;
  46.  
  47. struct class_level
  48. {
  49.   /* The previous class level.  */
  50.   struct class_level *level_chain;
  51.  
  52.   /* The class instance variable, as a PARM_DECL.  */
  53.   tree decl;
  54.   /* The class instance variable, as an object.  */
  55.   tree object;
  56.   /* The virtual function table pointer
  57.      for the class instance variable.  */
  58.   tree vtable_decl;
  59.  
  60.   /* Name of the current class.  */
  61.   tree name;
  62.   /* Type of the current class.  */
  63.   tree type;
  64.  
  65.   /* Flags for this class level.  */
  66.   int this_is_variable;
  67.   int memoized_lookups;
  68.   int save_memoized;
  69.   int unused;
  70. };
  71.  
  72. tree current_class_decl, C_C_D;    /* PARM_DECL: the class instance variable */
  73. tree current_vtable_decl;
  74.  
  75. /* The following two can be derived from the previous one */
  76. tree current_class_name;    /* IDENTIFIER_NODE: name of current class */
  77. tree current_class_type;    /* _TYPE: the type of the current class */
  78. tree previous_class_type;    /* _TYPE: the previous type that was a class */
  79. tree previous_class_values;        /* TREE_LIST: copy of the class_shadowed list
  80.                    when leaving an outermost class scope.  */
  81. static tree get_vfield_name PROTO((tree));
  82. tree the_null_vtable_entry;
  83.  
  84. /* Way of stacking language names.  */
  85. tree *current_lang_base, *current_lang_stack;
  86. int current_lang_stacksize;
  87.  
  88. /* Names of languages we recognize.  */
  89. tree lang_name_c, lang_name_cplusplus;
  90. tree current_lang_name;
  91.  
  92. /* When layout out an aggregate type, the size of the
  93.    basetypes (virtual and non-virtual) is passed to layout_record
  94.    via this node.  */
  95. static tree base_layout_decl;
  96.  
  97. /* Variables shared between class.c and call.c.  */
  98.  
  99. int n_vtables = 0;
  100. int n_vtable_entries = 0;
  101. int n_vtable_searches = 0;
  102. int n_vtable_elems = 0;
  103. int n_convert_harshness = 0;
  104. int n_compute_conversion_costs = 0;
  105. int n_build_method_call = 0;
  106. int n_inner_fields_searched = 0;
  107.  
  108. /* Virtual baseclass things.  */
  109. tree
  110. build_vbase_pointer (exp, type)
  111.      tree exp, type;
  112. {
  113.   char *name;
  114.  
  115.   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
  116.   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
  117.   return build_component_ref (exp, get_identifier (name), 0, 0);
  118. }
  119.  
  120. /* Is the type of the EXPR, the complete type of the object?
  121.    If we are going to be wrong, we must be conservative, and return 0. */
  122. int
  123. complete_type_p (expr)
  124.      tree expr;
  125. {
  126.   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
  127.   while (1)
  128.     {
  129.       switch (TREE_CODE (expr))
  130.     {
  131.     case SAVE_EXPR:
  132.     case INDIRECT_REF:
  133.     case ADDR_EXPR:
  134.     case NOP_EXPR:
  135.     case CONVERT_EXPR:
  136.       expr = TREE_OPERAND (expr, 0);
  137.       continue;
  138.  
  139.     case CALL_EXPR: 
  140.       if (! TREE_HAS_CONSTRUCTOR (expr))
  141.         break;
  142.       /* fall through... */
  143.     case VAR_DECL:
  144.     case FIELD_DECL:
  145.       if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
  146.           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
  147.           && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
  148.         return 1;
  149.       /* fall through... */
  150.     case TARGET_EXPR:
  151.     case PARM_DECL:
  152.       if (IS_AGGR_TYPE (TREE_TYPE (expr))
  153.           && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
  154.         return 1;
  155.       /* fall through... */
  156.     case PLUS_EXPR:
  157.     default:
  158.       break;
  159.     }
  160.       break;
  161.     }
  162.   return 0;
  163. }
  164.  
  165. /* Build multi-level access to EXPR using hierarchy path PATH.
  166.    CODE is PLUS_EXPR if we are going with the grain,
  167.    and MINUS_EXPR if we are not (in which case, we cannot traverse
  168.    virtual baseclass links).
  169.  
  170.    TYPE is the type we want this path to have on exit.
  171.  
  172.    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
  173. tree
  174. build_vbase_path (code, type, expr, path, alias_this)
  175.      enum tree_code code;
  176.      tree type, expr, path;
  177.      int alias_this;
  178. {
  179.   register int changed = 0;
  180.   tree last = NULL_TREE, last_virtual = NULL_TREE;
  181.   int nonnull = 0;
  182.   int fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
  183.   tree null_expr = 0, nonnull_expr;
  184.   tree basetype;
  185.   tree offset = integer_zero_node;
  186.  
  187.   /* We need additional logic to convert back to the unconverted type
  188.      (the static type of the complete object), and then convert back
  189.      to the type we want.  Until that is done, or until we can
  190.      recognize when that is, we cannot do the short cut logic. (mrs) */
  191.   /* Do this, until we can undo any previous convertions.  See net35.C
  192.      for a testcase. */
  193.   fixed_type_p = complete_type_p (expr);
  194.  
  195.   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
  196.     expr = save_expr (expr);
  197.   nonnull_expr = expr;
  198.  
  199.   if (BINFO_INHERITANCE_CHAIN (path))
  200.     {
  201.       tree reverse_path = NULL_TREE;
  202.  
  203.       while (path)
  204.     {
  205.       tree r = copy_node (path);
  206.       BINFO_INHERITANCE_CHAIN (r) = reverse_path;
  207.       reverse_path = r;
  208.       path = BINFO_INHERITANCE_CHAIN (path);
  209.     }
  210.       path = reverse_path;
  211.     }
  212.  
  213.   basetype = BINFO_TYPE (path);
  214.  
  215.   while (path)
  216.     {
  217.       if (TREE_VIA_VIRTUAL (path))
  218.     {
  219.       last_virtual = BINFO_TYPE (path);
  220.       if (code == PLUS_EXPR)
  221.         {
  222.           changed = ! fixed_type_p;
  223.  
  224.           if (changed)
  225.         {
  226.           extern int flag_assume_nonnull_objects;
  227.           tree ind;
  228.  
  229.           /* We already check for ambiguous things in the caller, just
  230.              find a path. */
  231.           if (last)
  232.             {
  233.               tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
  234.               nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
  235.             }
  236.           ind = build_indirect_ref (nonnull_expr, NULL_PTR);
  237.           nonnull_expr = build_vbase_pointer (ind, last_virtual);
  238.           if (nonnull == 0 && !flag_assume_nonnull_objects
  239.               && null_expr == NULL_TREE)
  240.             {
  241.               null_expr = build1 (NOP_EXPR, TYPE_POINTER_TO (last_virtual), integer_zero_node);
  242.               expr = build (COND_EXPR, TYPE_POINTER_TO (last_virtual),
  243.                     build (EQ_EXPR, integer_type_node, expr,
  244.                        integer_zero_node),
  245.                     null_expr, nonnull_expr);
  246.             }
  247.         }
  248.           /* else we'll figure out the offset below.  */
  249.  
  250.           /* Happens in the case of parse errors.  */
  251.           if (nonnull_expr == error_mark_node)
  252.         return error_mark_node;
  253.         }
  254.       else
  255.         {
  256.           cp_error ("cannot cast up from virtual baseclass `%T'",
  257.               last_virtual);
  258.           return error_mark_node;
  259.         }
  260.     }
  261.       last = path;
  262.       path = BINFO_INHERITANCE_CHAIN (path);
  263.     }
  264.   /* LAST is now the last basetype assoc on the path.  */
  265.  
  266.   /* A pointer to a virtual base member of a non-null object
  267.      is non-null.  Therefore, we only need to test for zeroness once.
  268.      Make EXPR the canonical expression to deal with here.  */
  269.   if (null_expr)
  270.     {
  271.       TREE_OPERAND (expr, 2) = nonnull_expr;
  272.       TREE_TYPE (TREE_OPERAND (expr, 1)) = TREE_TYPE (nonnull_expr);
  273.     }
  274.   else
  275.     expr = nonnull_expr;
  276.  
  277.   /* If we go through any virtual base pointers, make sure that
  278.      casts to BASETYPE from the last virtual base class use
  279.      the right value for BASETYPE.  */
  280.   if (changed)
  281.     {
  282.       tree intype = TREE_TYPE (TREE_TYPE (expr));
  283.       if (TYPE_MAIN_VARIANT (intype) == BINFO_TYPE (last))
  284.     basetype = intype;
  285.       else
  286.     {
  287.       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
  288.       basetype = last;
  289.       offset = BINFO_OFFSET (binfo);
  290.     }
  291.     }
  292.   else
  293.     {
  294.       if (last_virtual)
  295.     {
  296.       offset = BINFO_OFFSET (binfo_member (last_virtual,
  297.                            CLASSTYPE_VBASECLASSES (basetype)));
  298.       offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
  299.     }
  300.       else
  301.     offset = BINFO_OFFSET (last);
  302.     }
  303.  
  304.   if (TREE_INT_CST_LOW (offset))
  305.     {
  306.       /* For multiple inheritance: if `this' can be set by any
  307.      function, then it could be 0 on entry to any function.
  308.      Preserve such zeroness here.  Otherwise, only in the
  309.      case of constructors need we worry, and in those cases,
  310.      it will be zero, or initialized to some legal value to
  311.      which we may add.  */
  312.       if (nonnull == 0 && (alias_this == 0 || flag_this_is_variable > 0))
  313.     {
  314.       if (null_expr)
  315.         TREE_TYPE (null_expr) = type;
  316.       else
  317.         null_expr = build1 (NOP_EXPR, type, integer_zero_node);
  318.       if (TREE_SIDE_EFFECTS (expr))
  319.         expr = save_expr (expr);
  320.  
  321.       return build (COND_EXPR, type,
  322.             build (EQ_EXPR, integer_type_node, expr, integer_zero_node),
  323.             null_expr,
  324.             build (code, type, expr, offset));
  325.     }
  326.       else return build (code, type, expr, offset);
  327.     }
  328.  
  329.   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
  330.      be used multiple times in initialization of multiple inheritance.  */
  331.   if (null_expr)
  332.     {
  333.       TREE_TYPE (expr) = type;
  334.       return expr;
  335.     }
  336.   else
  337.     return build1 (NOP_EXPR, type, expr);
  338. }
  339.  
  340. /* Virtual function things.  */
  341.  
  342. /* Virtual functions to be dealt with after laying out our base
  343.    classes.  We do all overrides after we layout virtual base classes.
  344.    */
  345. static tree pending_hard_virtuals;
  346. static int doing_hard_virtuals;
  347.  
  348. /* Build an entry in the virtual function table.
  349.    DELTA is the offset for the `this' pointer.
  350.    PFN is an ADDR_EXPR containing a pointer to the virtual function.
  351.    Note that the index (DELTA2) in the virtual function table
  352.    is always 0.  */
  353. tree
  354. build_vtable_entry (delta, pfn)
  355.      tree delta, pfn;
  356. {
  357.  
  358.   if (flag_vtable_thunks)
  359.     {
  360.       HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
  361.       extern tree make_thunk ();
  362.       if (idelta)
  363.     {
  364.       pfn = build1 (ADDR_EXPR, vtable_entry_type,
  365.             make_thunk (pfn, idelta));
  366.       TREE_READONLY (pfn) = 1;
  367.       TREE_CONSTANT (pfn) = 1;
  368.     }
  369. #ifdef GATHER_STATISTICS
  370.       n_vtable_entries += 1;
  371. #endif
  372.       return pfn;
  373.     }
  374.   else
  375.     {
  376.       extern int flag_huge_objects;
  377.       tree elems = tree_cons (NULL_TREE, delta,
  378.                   tree_cons (NULL_TREE, integer_zero_node,
  379.                      build_tree_list (NULL_TREE, pfn)));
  380.       tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
  381.  
  382.       /* DELTA is constructed by `size_int', which means it may be an
  383.      unsigned quantity on some platforms.  Therefore, we cannot use
  384.      `int_fits_type_p', because when DELTA is really negative,
  385.      `force_fit_type' will make it look like a very large number.  */
  386.  
  387.       if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node))
  388.        < TREE_INT_CST_LOW (delta))
  389.       || (TREE_INT_CST_LOW (delta)
  390.           < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node))))
  391.     if (flag_huge_objects)
  392.       sorry ("object size exceeds built-in limit for virtual function table implementation");
  393.     else
  394.       sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
  395.  
  396.       TREE_CONSTANT (entry) = 1;
  397.       TREE_STATIC (entry) = 1;
  398.       TREE_READONLY (entry) = 1;
  399.  
  400. #ifdef GATHER_STATISTICS
  401.       n_vtable_entries += 1;
  402. #endif
  403.  
  404.       return entry;
  405.     }
  406. }
  407.  
  408. /* Given an object INSTANCE, return an expression which yields the
  409.    virtual function corresponding to INDEX.  There are many special
  410.    cases for INSTANCE which we take care of here, mainly to avoid
  411.    creating extra tree nodes when we don't have to.  */
  412. tree
  413. build_vfn_ref (ptr_to_instptr, instance, idx)
  414.      tree *ptr_to_instptr, instance;
  415.      tree idx;
  416. {
  417.   extern int building_cleanup;
  418.   tree vtbl, aref;
  419.   tree basetype = TREE_TYPE (instance);
  420.  
  421.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  422.     basetype = TREE_TYPE (basetype);
  423.  
  424.   if (instance == C_C_D)
  425.     {
  426.       if (current_vtable_decl == NULL_TREE
  427.       || current_vtable_decl == error_mark_node
  428.       || !UNIQUELY_DERIVED_FROM_P (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype))
  429.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), NULL_PTR);
  430.       else
  431.     vtbl = current_vtable_decl;
  432.     }
  433.   else
  434.     {
  435.       if (optimize)
  436.     {
  437.       /* Try to figure out what a reference refers to, and
  438.          access its virtual function table directly.  */
  439.       tree ref = NULL_TREE;
  440.  
  441.       if (TREE_CODE (instance) == INDIRECT_REF
  442.           && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
  443.         ref = TREE_OPERAND (instance, 0);
  444.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  445.         ref = instance;
  446.  
  447.       if (ref && TREE_CODE (ref) == VAR_DECL
  448.           && DECL_INITIAL (ref))
  449.         {
  450.           tree init = DECL_INITIAL (ref);
  451.  
  452.           while (TREE_CODE (init) == NOP_EXPR
  453.              || TREE_CODE (init) == NON_LVALUE_EXPR)
  454.         init = TREE_OPERAND (init, 0);
  455.           if (TREE_CODE (init) == ADDR_EXPR)
  456.         {
  457.           init = TREE_OPERAND (init, 0);
  458.           if (IS_AGGR_TYPE (TREE_TYPE (init))
  459.               && (TREE_CODE (init) == PARM_DECL
  460.               || TREE_CODE (init) == VAR_DECL))
  461.             instance = init;
  462.         }
  463.         }
  464.     }
  465.  
  466.       if (IS_AGGR_TYPE (TREE_TYPE (instance))
  467.       && !IS_SIGNATURE_POINTER (TREE_TYPE (instance))
  468.       && !IS_SIGNATURE_REFERENCE (TREE_TYPE (instance))
  469.       && (TREE_CODE (instance) == RESULT_DECL
  470.           || TREE_CODE (instance) == PARM_DECL
  471.           || TREE_CODE (instance) == VAR_DECL))
  472.     vtbl = TYPE_BINFO_VTABLE (basetype);
  473.       else
  474.     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
  475.                    NULL_PTR);
  476.     }
  477.   if (!flag_vtable_thunks)
  478.     assemble_external (vtbl);
  479.   aref = build_array_ref (vtbl, idx);
  480.  
  481.   /* Save the intermediate result in a SAVE_EXPR so we don't have to
  482.      compute each component of the virtual function pointer twice.  */ 
  483.   if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
  484.     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
  485.  
  486.   if (flag_vtable_thunks)
  487.     return aref;
  488.   else
  489.     {
  490.       *ptr_to_instptr
  491.     = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
  492.          *ptr_to_instptr,
  493.          convert (ptrdiff_type_node,
  494.               build_component_ref (aref, delta_identifier, 0, 0)));
  495.       return build_component_ref (aref, pfn_identifier, 0, 0);
  496.     }
  497. }
  498.  
  499. /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
  500.    for the given TYPE.  */
  501. static tree
  502. get_vtable_name (type)
  503.      tree type;
  504. {
  505.   tree type_id = build_typename_overload (type);
  506.   char *buf = (char *)alloca (strlen (VTABLE_NAME_FORMAT)
  507.                   + IDENTIFIER_LENGTH (type_id) + 2);
  508.   char *ptr = IDENTIFIER_POINTER (type_id);
  509.   int i;
  510.   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
  511. #if 0
  512.   /* We don't take off the numbers; prepare_fresh_vtable uses the
  513.      DECL_ASSEMBLER_NAME for the type, which includes the number
  514.      in `3foo'.  If we were to pull them off here, we'd end up with
  515.      something like `_vt.foo.3bar', instead of a uniform definition.  */
  516.   while (ptr[i] >= '0' && ptr[i] <= '9')
  517.     i += 1;
  518. #endif
  519.   sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
  520.   return get_identifier (buf);
  521. }
  522.  
  523. /* Build a virtual function for type TYPE.
  524.    If BINFO is non-NULL, build the vtable starting with the initial
  525.    approximation that it is the same as the one which is the head of
  526.    the association list.  */
  527. static tree
  528. build_vtable (binfo, type)
  529.      tree binfo, type;
  530. {
  531.   tree name = get_vtable_name (type);
  532.   tree virtuals, decl;
  533.  
  534.   if (binfo)
  535.     {
  536.       virtuals = copy_list (BINFO_VIRTUALS (binfo));
  537.       decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
  538.     }
  539.   else
  540.     {
  541.       virtuals = NULL_TREE;
  542.       decl = build_decl (VAR_DECL, name, void_type_node);
  543.     }
  544.  
  545. #ifdef GATHER_STATISTICS
  546.   n_vtables += 1;
  547.   n_vtable_elems += list_length (virtuals);
  548. #endif
  549.  
  550.   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
  551.   if (! flag_vtable_thunks)
  552.     import_export_vtable (decl, type);
  553.  
  554.   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
  555.   /* Initialize the association list for this type, based
  556.      on our first approximation.  */
  557.   TYPE_BINFO_VTABLE (type) = decl;
  558.   TYPE_BINFO_VIRTUALS (type) = virtuals;
  559.  
  560.   TREE_STATIC (decl) = 1;
  561. #ifndef WRITABLE_VTABLES
  562.   /* Make them READONLY by default. (mrs) */
  563.   TREE_READONLY (decl) = 1;
  564. #endif
  565.   /* At one time the vtable info was grabbed 2 words at a time.  This
  566.      fails on sparc unless you have 8-byte alignment.  (tiemann) */
  567.   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  568.                DECL_ALIGN (decl));
  569.  
  570.   /* Why is this conditional? (mrs) */
  571.   if (binfo && write_virtuals >= 0)
  572.     DECL_VIRTUAL_P (decl) = 1;
  573.   DECL_CONTEXT (decl) = type;
  574.  
  575.   binfo = TYPE_BINFO (type);
  576.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  577.   return decl;
  578. }
  579.  
  580. /* Given a base type PARENT, and a derived type TYPE, build
  581.    a name which distinguishes exactly the PARENT member of TYPE's type.
  582.  
  583.    FORMAT is a string which controls how sprintf formats the name
  584.    we have generated.
  585.  
  586.    For example, given
  587.  
  588.     class A; class B; class C : A, B;
  589.  
  590.    it is possible to distinguish "A" from "C's A".  And given
  591.  
  592.     class L;
  593.     class A : L; class B : L; class C : A, B;
  594.  
  595.    it is possible to distinguish "L" from "A's L", and also from
  596.    "C's L from A".
  597.  
  598.    Make sure to use the DECL_ASSEMBLER_NAME of the TYPE_NAME of the
  599.    type, as template have DECL_NAMEs like: X<int>, whereas the
  600.    DECL_ASSEMBLER_NAME is set to be something the assembler can handle.
  601.   */
  602. static tree
  603. build_type_pathname (format, parent, type)
  604.      char *format;
  605.      tree parent, type;
  606. {
  607.   extern struct obstack temporary_obstack;
  608.   char *first, *base, *name;
  609.   int i;
  610.   tree id;
  611.  
  612.   parent = TYPE_MAIN_VARIANT (parent);
  613.  
  614.   /* Remember where to cut the obstack to.  */
  615.   first = obstack_base (&temporary_obstack);
  616.  
  617.   /* Put on TYPE+PARENT.  */
  618.   obstack_grow (&temporary_obstack,
  619.         TYPE_ASSEMBLER_NAME_STRING (type),
  620.         TYPE_ASSEMBLER_NAME_LENGTH (type));
  621. #ifdef JOINER
  622.   obstack_1grow (&temporary_obstack, JOINER);
  623. #else
  624.   obstack_1grow (&temporary_obstack, '_');
  625. #endif
  626.   obstack_grow0 (&temporary_obstack,
  627.          TYPE_ASSEMBLER_NAME_STRING (parent),
  628.          TYPE_ASSEMBLER_NAME_LENGTH (parent));
  629.   i = obstack_object_size (&temporary_obstack);
  630.   base = obstack_base (&temporary_obstack);
  631.   obstack_finish (&temporary_obstack);
  632.  
  633.   /* Put on FORMAT+TYPE+PARENT.  */
  634.   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
  635.   name = obstack_base (&temporary_obstack);
  636.   sprintf (name, format, base);
  637.   id = get_identifier (name);
  638.   obstack_free (&temporary_obstack, first);
  639.  
  640.   return id;
  641. }
  642.  
  643. /* Give TYPE a new virtual function table which is initialized
  644.    with a skeleton-copy of its original initialization.  The only
  645.    entry that changes is the `delta' entry, so we can really
  646.    share a lot of structure.
  647.  
  648.    FOR_TYPE is the derived type which caused this table to
  649.    be needed.
  650.  
  651.    BINFO is the type association which provided TYPE for FOR_TYPE.  */
  652. static void
  653. prepare_fresh_vtable (binfo, for_type)
  654.      tree binfo, for_type;
  655. {
  656.   tree basetype = BINFO_TYPE (binfo);
  657.   tree orig_decl = BINFO_VTABLE (binfo);
  658.   /* This name is too simplistic.  We can have multiple basetypes for
  659.      for_type, and we really want different names.  (mrs) */
  660.   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
  661.   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
  662.   tree path;
  663.   int result;
  664.  
  665.   /* Remember which class this vtable is really for.  */
  666.   DECL_CONTEXT (new_decl) = for_type;
  667.  
  668.   TREE_STATIC (new_decl) = 1;
  669.   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
  670.   DECL_VIRTUAL_P (new_decl) = 1;
  671. #ifndef WRITABLE_VTABLES
  672.   /* Make them READONLY by default. (mrs) */
  673.   TREE_READONLY (new_decl) = 1;
  674. #endif
  675.   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
  676.  
  677.   /* Make fresh virtual list, so we can smash it later.  */
  678.   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
  679.   /* Install the value for `headof' if that's what we're doing.  */
  680.   if (flag_dossier)
  681.     TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))
  682.       = build_vtable_entry (size_binop (MINUS_EXPR, integer_zero_node, BINFO_OFFSET (binfo)),
  683.                 FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (TREE_CHAIN (BINFO_VIRTUALS (binfo)))));
  684.  
  685. #ifdef GATHER_STATISTICS
  686.   n_vtables += 1;
  687.   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
  688. #endif
  689.  
  690.   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
  691.   if (! flag_vtable_thunks)
  692.     import_export_vtable (new_decl, for_type);
  693.  
  694.   if (TREE_VIA_VIRTUAL (binfo))
  695.     my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
  696.                    CLASSTYPE_VBASECLASSES (current_class_type)),
  697.             170);
  698.   SET_BINFO_NEW_VTABLE_MARKED (binfo);
  699. }
  700.  
  701. /* Access the virtual function table entry that logically
  702.    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
  703.    initializer.  We can run off the end, when dealing with virtual
  704.    destructors in MI situations, return NULL_TREE in that case.  */
  705. static tree
  706. get_vtable_entry (virtuals, base_fndecl)
  707.      tree virtuals, base_fndecl;
  708. {
  709.   unsigned HOST_WIDE_INT i = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
  710.        ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
  711.           & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
  712.        : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
  713.  
  714. #ifdef GATHER_STATISTICS
  715.   n_vtable_searches += i;
  716. #endif
  717.  
  718.   while (i > 0 && virtuals)
  719.     {
  720.       virtuals = TREE_CHAIN (virtuals);
  721.       i -= 1;
  722.     }
  723.   return virtuals;
  724. }
  725.  
  726. /* Put new entry ENTRY into virtual function table initializer
  727.    VIRTUALS.
  728.  
  729.    Also update DECL_VINDEX (FNDECL).  */
  730.  
  731. static void
  732. modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
  733.      tree old_entry_in_list, new_entry, fndecl;
  734. {
  735.   tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
  736.  
  737. #ifdef NOTQUITE
  738.   cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
  739.           DECL_ASSEMBLER_NAME (fndecl));
  740. #endif
  741.   TREE_VALUE (old_entry_in_list) = new_entry;
  742.  
  743.   /* Now assign virtual dispatch information, if unset.  */
  744.   /* We can dispatch this, through any overridden base function. */
  745.   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  746.     {
  747.       DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
  748.       DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
  749.     }
  750. }
  751.  
  752. /* Access the virtual function table entry i.  VIRTUALS is the virtual
  753.    function table's initializer.  */
  754. static tree
  755. get_vtable_entry_n (virtuals, i)
  756.      tree virtuals;
  757.      unsigned HOST_WIDE_INT i;
  758. {
  759.   while (i > 0)
  760.     {
  761.       virtuals = TREE_CHAIN (virtuals);
  762.       i -= 1;
  763.     }
  764.   return virtuals;
  765. }
  766.  
  767. /* Add a virtual function to all the appropriate vtables for the class
  768.    T.  DECL_VINDEX(X) should be error_mark_node, if we want to
  769.    allocate a new slot in our table.  If it is error_mark_node, we
  770.    know that no other function from another vtable is overridden by X.
  771.    HAS_VIRTUAL keeps track of how many virtuals there are in our main
  772.    vtable for the type, and we build upon the PENDING_VIRTUALS list
  773.    and return it.  */
  774. static tree
  775. add_virtual_function (pending_virtuals, has_virtual, fndecl, t)
  776.      tree pending_virtuals;
  777.      int *has_virtual;
  778.      tree fndecl;
  779.      tree t; /* Structure type. */
  780. {
  781.   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
  782.      convert to void *.  Make such a conversion here.  */
  783.   tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
  784.   TREE_CONSTANT (vfn) = 1;
  785.  
  786. #ifndef DUMB_USER
  787.   if (current_class_type == 0)
  788.     cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
  789.         fndecl);
  790.   if (current_class_type && t != current_class_type)
  791.     cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
  792.         fndecl);
  793. #endif
  794.  
  795.   if (!flag_vtable_thunks)
  796.     TREE_ADDRESSABLE (fndecl) = CLASSTYPE_VTABLE_NEEDS_WRITING (t);
  797.  
  798.   /* If the virtual function is a redefinition of a prior one,
  799.      figure out in which base class the new definition goes,
  800.      and if necessary, make a fresh virtual function table
  801.      to hold that entry.  */
  802.   if (DECL_VINDEX (fndecl) == error_mark_node)
  803.     {
  804.       tree entry;
  805.  
  806.       if (flag_dossier && *has_virtual == 0)
  807.     {
  808.       /* CLASSTYPE_DOSSIER is only used as a Boolean (NULL or not). */
  809.       CLASSTYPE_DOSSIER (t) = integer_one_node;
  810.       *has_virtual = 1;
  811.         }
  812.  
  813.       /* Build a new INT_CST for this DECL_VINDEX.  */
  814.       {
  815.     static tree index_table[256];
  816.     tree index;
  817.     int i = ++(*has_virtual);
  818.  
  819.     if (i >= 256 || index_table[i] == 0)
  820.       {
  821.         index = build_int_2 (i, 0);
  822.         if (i < 256)
  823.           index_table[i] = index;
  824.       }
  825.     else
  826.       index = index_table[i];
  827.  
  828.     /* Now assign virtual dispatch information. */
  829.     DECL_VINDEX (fndecl) = index;
  830.     DECL_CONTEXT (fndecl) = t;
  831.       }
  832.       entry = build_vtable_entry (integer_zero_node, vfn);
  833.       pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
  834.     }
  835.   /* Might already be INTEGER_CST if declared twice in class.  We will
  836.      give error later or we've already given it.  */
  837.   else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
  838.     {
  839.       /* Need an entry in some other virtual function table.
  840.          Deal with this after we have laid out our virtual base classes.  */
  841.       pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
  842.     }
  843.   return pending_virtuals;
  844. }
  845.  
  846. /* Obstack on which to build the vector of class methods.  */
  847. struct obstack class_obstack;
  848. extern struct obstack *current_obstack;
  849.  
  850. /* Add method METHOD to class TYPE.  This is used when a method
  851.    has been defined which did not initially appear in the class definition,
  852.    and helps cut down on spurious error messages.
  853.  
  854.    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
  855.    the method should be added.  */
  856. void
  857. add_method (type, fields, method)
  858.      tree type, *fields, method;
  859. {
  860.   /* We must make a copy of METHOD here, since we must be sure that
  861.      we have exclusive title to this method's DECL_CHAIN.  */
  862.   tree decl;
  863.  
  864.   push_obstacks (&permanent_obstack, &permanent_obstack);
  865.   {
  866.     decl = copy_node (method);
  867.     if (DECL_RTL (decl) == 0
  868.         && (!processing_template_decl
  869.             || !uses_template_parms (decl)))
  870.       {
  871.     make_function_rtl (decl);
  872.     DECL_RTL (method) = DECL_RTL (decl);
  873.       }
  874.   }
  875.  
  876.   if (fields && *fields)
  877.     {
  878.       /* Take care not to hide destructor.  */
  879.       DECL_CHAIN (decl) = DECL_CHAIN (*fields);
  880.       DECL_CHAIN (*fields) = decl;
  881.     }
  882.   else if (CLASSTYPE_METHOD_VEC (type) == 0)
  883.     {
  884.       tree method_vec = make_node (TREE_VEC);
  885.       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
  886.     {
  887.       TREE_VEC_ELT (method_vec, 0) = decl;
  888.       TREE_VEC_LENGTH (method_vec) = 1;
  889.     }
  890.       else
  891.     {
  892.       /* ??? Is it possible for there to have been enough room in the
  893.          current chunk for the tree_vec structure but not a tree_vec
  894.          plus a tree*?  Will this work in that case?  */
  895.       obstack_free (current_obstack, method_vec);
  896.       obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
  897.       TREE_VEC_ELT (method_vec, 1) = decl;
  898.       TREE_VEC_LENGTH (method_vec) = 2;
  899.       obstack_finish (current_obstack);
  900.     }
  901.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  902.     }
  903.   else
  904.     {
  905.       tree method_vec = CLASSTYPE_METHOD_VEC (type);
  906.       int len = TREE_VEC_LENGTH (method_vec);
  907.  
  908.       /* Adding a new ctor or dtor.  This is easy because our
  909.          METHOD_VEC always has a slot for such entries.  */
  910.       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
  911.     {
  912.       /* TREE_VEC_ELT (method_vec, 0) = decl; */
  913.       if (decl != TREE_VEC_ELT (method_vec, 0))
  914.         {
  915.           DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, 0);
  916.           TREE_VEC_ELT (method_vec, 0) = decl;
  917.         }
  918.     }
  919.       else
  920.     {
  921.       /* This is trickier.  We try to extend the TREE_VEC in-place,
  922.          but if that does not work, we copy all its data to a new
  923.          TREE_VEC that's large enough.  */
  924.       struct obstack *ob = &class_obstack;
  925.       tree *end = (tree *)obstack_next_free (ob);
  926.  
  927.       if (end != TREE_VEC_END (method_vec))
  928.         {
  929.           ob = current_obstack;
  930.           TREE_VEC_LENGTH (method_vec) += 1;
  931.           TREE_VEC_ELT (method_vec, len) = NULL_TREE;
  932.           method_vec = copy_node (method_vec);
  933.           TREE_VEC_LENGTH (method_vec) -= 1;
  934.         }
  935.       else
  936.         {
  937.           tree tmp_vec = (tree) obstack_base (ob);
  938.           if (obstack_room (ob) < sizeof (tree))
  939.         {
  940.           obstack_blank (ob, sizeof (struct tree_common)
  941.                  + tree_code_length[(int) TREE_VEC]
  942.                    * sizeof (char *)
  943.                  + len * sizeof (tree));
  944.           tmp_vec = (tree) obstack_base (ob);
  945.           bcopy (method_vec, tmp_vec,
  946.              (sizeof (struct tree_common)
  947.               + tree_code_length[(int) TREE_VEC] * sizeof (char *)
  948.               + (len-1) * sizeof (tree)));
  949.           method_vec = tmp_vec;
  950.         }
  951.           else
  952.         obstack_blank (ob, sizeof (tree));
  953.         }
  954.  
  955.       obstack_finish (ob);
  956.       TREE_VEC_ELT (method_vec, len) = decl;
  957.       TREE_VEC_LENGTH (method_vec) = len + 1;
  958.       CLASSTYPE_METHOD_VEC (type) = method_vec;
  959.  
  960.       if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
  961.         {
  962.           /* ??? May be better to know whether these can be extended?  */
  963.           tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
  964.  
  965.           TREE_VEC_LENGTH (baselink_vec) += 1;
  966.           CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
  967.           TREE_VEC_LENGTH (baselink_vec) -= 1;
  968.  
  969.           TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
  970.         }
  971.     }
  972.     }
  973.   DECL_CONTEXT (decl) = type;
  974.   DECL_CLASS_CONTEXT (decl) = type;
  975.  
  976.   pop_obstacks ();
  977. }
  978.  
  979. /* Subroutines of finish_struct.  */
  980.  
  981. /* Look through the list of fields for this struct, deleting
  982.    duplicates as we go.  This must be recursive to handle
  983.    anonymous unions.
  984.  
  985.    FIELD is the field which may not appear anywhere in FIELDS.
  986.    FIELD_PTR, if non-null, is the starting point at which
  987.    chained deletions may take place.
  988.    The value returned is the first acceptable entry found
  989.    in FIELDS.
  990.  
  991.    Note that anonymous fields which are not of UNION_TYPE are
  992.    not duplicates, they are just anonymous fields.  This happens
  993.    when we have unnamed bitfields, for example.  */
  994. static tree
  995. delete_duplicate_fields_1 (field, field_ptr, fields)
  996.      tree field, *field_ptr, fields;
  997. {
  998.   tree x;
  999.   tree prev = field_ptr ? *field_ptr : 0;
  1000.   if (DECL_NAME (field) == 0)
  1001.     {
  1002.       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
  1003.     return fields;
  1004.  
  1005.       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
  1006.     fields = delete_duplicate_fields_1 (x, field_ptr, fields);
  1007.       if (prev)
  1008.     TREE_CHAIN (prev) = fields;
  1009.       return fields;
  1010.     }
  1011.   else
  1012.     {
  1013.       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
  1014.     {
  1015.       if (DECL_NAME (x) == 0)
  1016.         {
  1017.           if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
  1018.         continue;
  1019.           TYPE_FIELDS (TREE_TYPE (x))
  1020.         = delete_duplicate_fields_1 (field, (tree *)0, TYPE_FIELDS (TREE_TYPE (x)));
  1021.           if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
  1022.         {
  1023.           if (prev == 0)
  1024.             fields = TREE_CHAIN (fields);
  1025.           else
  1026.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1027.         }
  1028.         }
  1029.       else
  1030.         {
  1031.           if (DECL_NAME (field) == DECL_NAME (x))
  1032.         {
  1033.           if (TREE_CODE (field) == CONST_DECL
  1034.               && TREE_CODE (x) == CONST_DECL)
  1035.             cp_error_at ("duplicate enum value `%D'", x);
  1036.           else if (TREE_CODE (field) == CONST_DECL
  1037.                || TREE_CODE (x) == CONST_DECL)
  1038.             cp_error_at ("duplicate field `%D' (as enum and non-enum)",
  1039.                 x);
  1040.           else if (TREE_CODE (field) == TYPE_DECL
  1041.                && TREE_CODE (x) == TYPE_DECL)
  1042.             cp_error_at ("duplicate class scope type `%D'", x);
  1043.           else if (TREE_CODE (field) == TYPE_DECL
  1044.                || TREE_CODE (x) == TYPE_DECL)
  1045.             cp_error_at ("duplicate field `%D' (as type and non-type)",
  1046.                 x);
  1047.           else
  1048.             cp_error_at ("duplicate member `%D'", x);
  1049.           if (prev == 0)
  1050.             fields = TREE_CHAIN (fields);
  1051.           else
  1052.             TREE_CHAIN (prev) = TREE_CHAIN (x);
  1053.         }
  1054.         }
  1055.     }
  1056.     }
  1057.   return fields;
  1058. }
  1059.  
  1060. static void
  1061. delete_duplicate_fields (fields)
  1062.      tree fields;
  1063. {
  1064.   tree x;
  1065.   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
  1066.     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
  1067. }
  1068.  
  1069. /* Change the access of FDECL to ACCESS in T.
  1070.    Return 1 if change was legit, otherwise return 0.  */
  1071. static int
  1072. alter_access (t, fdecl, access)
  1073.      tree t;
  1074.      tree fdecl;
  1075.      enum access_type access;
  1076. {
  1077.   tree elem = purpose_member (t, DECL_ACCESS (fdecl));
  1078.   if (elem && TREE_VALUE (elem) != (tree)access)
  1079.     {
  1080.       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
  1081.     {
  1082.       cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
  1083.     }
  1084.       else
  1085.     error ("conflicting access specifications for field `%s', ignored",
  1086.            IDENTIFIER_POINTER (DECL_NAME (fdecl)));
  1087.     }
  1088.   else if (TREE_PRIVATE (fdecl) && access != access_private)
  1089.     cp_error_at ("cannot make private `%D' non-private", fdecl);
  1090.   else if (TREE_PROTECTED (fdecl))
  1091.     {
  1092.       if (access == access_public)
  1093.     cp_error_at ("cannot make protected `%D' public", fdecl);
  1094.       goto alter;
  1095.     }
  1096.   /* ARM 11.3: an access declaration may not be used to restrict access
  1097.      to a member that is accessible in the base class.  */
  1098.   else if (TREE_PUBLIC (fdecl)
  1099.        && (access == access_private
  1100.            || access == access_protected))
  1101.     cp_error_at ("cannot reduce access of public member `%D'", fdecl);
  1102.   else if (elem == NULL_TREE)
  1103.     {
  1104.     alter:
  1105.       DECL_ACCESS (fdecl) = tree_cons (t, (tree)access,
  1106.                        DECL_ACCESS (fdecl));
  1107.       return 1;
  1108.     }
  1109.   return 0;
  1110. }
  1111.  
  1112. /* Return the offset to the main vtable for a given base BINFO.  */
  1113. tree
  1114. get_vfield_offset (binfo)
  1115.      tree binfo;
  1116. {
  1117.   return size_binop (PLUS_EXPR,
  1118.              size_binop (FLOOR_DIV_EXPR,
  1119.                  DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
  1120.                  size_int (BITS_PER_UNIT)),
  1121.              BINFO_OFFSET (binfo));
  1122. }
  1123.  
  1124. /* Get the offset to the start of the original binfo that we derived this
  1125.    binfo from.  */
  1126. tree get_derived_offset (binfo)
  1127.      tree binfo;
  1128. {
  1129.   tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
  1130.   tree offset2;
  1131.   int i;
  1132.   while (BINFO_BASETYPES (binfo)
  1133.      && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
  1134.     {
  1135.       tree binfos = BINFO_BASETYPES (binfo);
  1136.       binfo = TREE_VEC_ELT (binfos, i);
  1137.     }
  1138.   offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
  1139.   return size_binop (MINUS_EXPR, offset1, offset2);
  1140. }
  1141.  
  1142. /* If FOR_TYPE needs to reinitialize virtual function table pointers
  1143.    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
  1144.    Returns BASE_INIT_LIST appropriately modified.  */
  1145.  
  1146. static tree
  1147. maybe_fixup_vptrs (for_type, binfo, base_init_list)
  1148.      tree for_type, binfo, base_init_list;
  1149. {
  1150.   /* Now reinitialize any slots that don't fall under our virtual
  1151.      function table pointer.  */
  1152.   tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
  1153.   while (vfields)
  1154.     {
  1155.       tree basetype = VF_NORMAL_VALUE (vfields)
  1156.     ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
  1157.       : VF_BASETYPE_VALUE (vfields);
  1158.  
  1159.       tree base_binfo = get_binfo (basetype, for_type, 0);
  1160.       /* Punt until this is implemented. */
  1161.       if (1 /* BINFO_MODIFIED (base_binfo) */)
  1162.     {
  1163.       tree base_offset = get_vfield_offset (base_binfo);
  1164.       if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
  1165.           && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
  1166.         base_init_list = tree_cons (error_mark_node, base_binfo,
  1167.                     base_init_list);
  1168.     }
  1169.       vfields = TREE_CHAIN (vfields);
  1170.     }
  1171.   return base_init_list;
  1172. }
  1173.  
  1174. /* If TYPE does not have a constructor, then the compiler must
  1175.    manually deal with all of the initialization this type requires.
  1176.  
  1177.    If a base initializer exists only to fill in the virtual function
  1178.    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
  1179.    This way, we avoid multiple initializations of the same field by
  1180.    each virtual function table up the class hierarchy.
  1181.  
  1182.    Virtual base class pointers are not initialized here.  They are
  1183.    initialized only at the "top level" of object creation.  If we
  1184.    initialized them here, we would have to skip a lot of work.  */
  1185.  
  1186. static void
  1187. build_class_init_list (type)
  1188.      tree type;
  1189. {
  1190.   tree base_init_list = NULL_TREE;
  1191.   tree member_init_list = NULL_TREE;
  1192.  
  1193.   /* Since we build member_init_list and base_init_list using
  1194.      tree_cons, backwards fields the all through work.  */
  1195.   tree x;
  1196.   tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
  1197.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1198.  
  1199.   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
  1200.     {
  1201.       if (TREE_CODE (x) != FIELD_DECL)
  1202.     continue;
  1203.  
  1204.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
  1205.       || DECL_INITIAL (x) != NULL_TREE)
  1206.     member_init_list = tree_cons (x, type, member_init_list);
  1207.     }
  1208.   member_init_list = nreverse (member_init_list);
  1209.  
  1210.   /* We will end up doing this last.  Need special marker
  1211.      to avoid infinite regress.  */
  1212.   if (TYPE_VIRTUAL_P (type))
  1213.     {
  1214.       base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
  1215.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
  1216.     TREE_VALUE (base_init_list) = NULL_TREE;
  1217.       TREE_ADDRESSABLE (base_init_list) = 1;
  1218.     }
  1219.  
  1220.   /* Each base class which needs to have initialization
  1221.      of some kind gets to make such requests known here.  */
  1222.   for (i = n_baseclasses-1; i >= 0; i--)
  1223.     {
  1224.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1225.       tree blist;
  1226.  
  1227.       /* Don't initialize virtual baseclasses this way.  */
  1228.       if (TREE_VIA_VIRTUAL (base_binfo))
  1229.     continue;
  1230.  
  1231.       if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
  1232.     {
  1233.       /* ...and the last shall come first...  */
  1234.       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
  1235.       base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
  1236.       continue;
  1237.     }
  1238.  
  1239.       if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
  1240.     /* Nothing to initialize.  */
  1241.     continue;
  1242.  
  1243.       /* ...ditto...  */
  1244.       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
  1245.  
  1246.       /* This is normally true for single inheritance.
  1247.      The win is we can shrink the chain of initializations
  1248.      to be done by only converting to the actual type
  1249.      we are interested in.  */
  1250.       if (TREE_VALUE (blist)
  1251.       && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
  1252.       && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
  1253.                  BINFO_OFFSET (TREE_VALUE (blist))))
  1254.     {
  1255.       if (base_init_list)
  1256.         {
  1257.           /* Does it do more than just fill in a
  1258.          virtual function table pointer?  */
  1259.           if (! TREE_ADDRESSABLE (blist))
  1260.         base_init_list = build_tree_list (blist, base_init_list);
  1261.           /* Can we get by just with the virtual function table
  1262.          pointer that it fills in?  */
  1263.           else if (TREE_ADDRESSABLE (base_init_list)
  1264.                && TREE_VALUE (base_init_list) == 0)
  1265.         base_init_list = blist;
  1266.           /* Maybe, but it is not obvious as the previous case.  */
  1267.           else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
  1268.         {
  1269.           tree last = tree_last (base_init_list);
  1270.           while (TREE_VALUE (last)
  1271.              && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
  1272.             last = tree_last (TREE_VALUE (last));
  1273.           if (TREE_VALUE (last) == 0)
  1274.             base_init_list = build_tree_list (blist, base_init_list);
  1275.         }
  1276.         }
  1277.       else
  1278.         base_init_list = blist;
  1279.     }
  1280.       else
  1281.     {
  1282.       /* The function expand_aggr_init knows how to do the
  1283.          initialization of `basetype' without getting
  1284.          an explicit `blist'.  */
  1285.       if (base_init_list)
  1286.         base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
  1287.       else
  1288.         base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
  1289.     }
  1290.     }
  1291.  
  1292.   if (base_init_list)
  1293.     if (member_init_list)
  1294.       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
  1295.     else
  1296.       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
  1297.   else if (member_init_list)
  1298.     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
  1299. }
  1300.  
  1301. struct base_info
  1302. {
  1303.   int has_virtual;
  1304.   int max_has_virtual;
  1305.   int n_ancestors;
  1306.   tree vfield;
  1307.   tree vfields;
  1308.   char cant_have_default_ctor;
  1309.   char cant_have_const_ctor;
  1310.   char cant_synth_copy_ctor;
  1311.   char cant_synth_asn_ref;
  1312.   char no_const_asn_ref;
  1313.   char needs_virtual_dtor;
  1314. };
  1315.  
  1316. /* Record information about type T derived from its base classes.
  1317.    Store most of that information in T itself, and place the
  1318.    remaining information in the struct BASE_INFO.
  1319.  
  1320.    Propagate basetype offsets throughout the lattice.  Note that the
  1321.    lattice topped by T is really a pair: it's a DAG that gives the
  1322.    structure of the derivation hierarchy, and it's a list of the
  1323.    virtual baseclasses that appear anywhere in the DAG.  When a vbase
  1324.    type appears in the DAG, it's offset is 0, and it's children start
  1325.    their offsets from that point.  When a vbase type appears in the list,
  1326.    its offset is the offset it has in the hierarchy, and its children's
  1327.    offsets include that offset in theirs.
  1328.  
  1329.    Returns the index of the first base class to have virtual functions,
  1330.    or -1 if no such base class.
  1331.  
  1332.    Note that at this point TYPE_BINFO (t) != t_binfo.  */
  1333.  
  1334. static int
  1335. finish_base_struct (t, b, t_binfo)
  1336.      tree t;
  1337.      struct base_info *b;
  1338.      tree t_binfo;
  1339. {
  1340.   tree binfos = BINFO_BASETYPES (t_binfo);
  1341.   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1342.   int first_vfn_base_index = -1;
  1343.   bzero (b, sizeof (struct base_info));
  1344.  
  1345.   for (i = 0; i < n_baseclasses; i++)
  1346.     {
  1347.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1348.       tree basetype = BINFO_TYPE (base_binfo);
  1349.  
  1350.       /* If the type of basetype is incomplete, then
  1351.      we already complained about that fact
  1352.      (and we should have fixed it up as well).  */
  1353.       if (TYPE_SIZE (basetype) == 0)
  1354.     {
  1355.       int j;
  1356.       /* The base type is of incomplete type.  It is
  1357.          probably best to pretend that it does not
  1358.          exist.  */
  1359.       if (i == n_baseclasses-1)
  1360.         TREE_VEC_ELT (binfos, i) = NULL_TREE;
  1361.       TREE_VEC_LENGTH (binfos) -= 1;
  1362.       n_baseclasses -= 1;
  1363.       for (j = i; j+1 < n_baseclasses; j++)
  1364.         TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
  1365.     }
  1366.  
  1367.       if (TYPE_HAS_INIT_REF (basetype)
  1368.       && !TYPE_HAS_CONST_INIT_REF (basetype))
  1369.     b->cant_have_const_ctor = 1;
  1370.       if (! TYPE_HAS_INIT_REF (basetype)
  1371.       || (TYPE_HAS_NONPUBLIC_CTOR (basetype) == 2
  1372.           && ! is_friend_type (t, basetype)))
  1373.     b->cant_synth_copy_ctor = 1;
  1374.  
  1375.       if (TYPE_HAS_CONSTRUCTOR (basetype)
  1376.       && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
  1377.     {
  1378.       b->cant_have_default_ctor = 1;
  1379.       if (! TYPE_HAS_CONSTRUCTOR (t))
  1380.         {
  1381.           cp_pedwarn ("base `%T' with only non-default constructor",
  1382.               basetype);
  1383.           cp_pedwarn ("in class without a constructor");
  1384.         }
  1385.     }
  1386.  
  1387.       if (TYPE_HAS_ASSIGN_REF (basetype)
  1388.       && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
  1389.     b->no_const_asn_ref = 1;
  1390.       if (! TYPE_HAS_ASSIGN_REF (basetype)
  1391.       || TYPE_HAS_ABSTRACT_ASSIGN_REF (basetype)
  1392.       || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (basetype) == 2
  1393.           && ! is_friend_type (t, basetype)))
  1394.     b->cant_synth_asn_ref = 1;
  1395.  
  1396.       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
  1397.       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
  1398.       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
  1399.       TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
  1400.       TYPE_HAS_COMPLEX_INIT_REF (t) |= (TYPE_HAS_COMPLEX_INIT_REF (basetype)
  1401.                     || TYPE_NEEDS_CONSTRUCTING (basetype));
  1402.  
  1403.       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
  1404.       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
  1405.       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
  1406.  
  1407.       if (! TREE_VIA_VIRTUAL (base_binfo)
  1408. #if 0
  1409.       /* This cannot be done, as prepare_fresh_vtable wants to modify
  1410.          binfos associated with vfields anywhere in the hierarchy, not
  1411.          just immediate base classes.  Due to unsharing, the compiler
  1412.          might consume 3% more memory on a real program.
  1413.          */
  1414.       && ! BINFO_OFFSET_ZEROP (base_binfo)
  1415. #endif
  1416.       && BINFO_BASETYPES (base_binfo))
  1417.     {
  1418.       tree base_binfos = BINFO_BASETYPES (base_binfo);
  1419.       tree chain = NULL_TREE;
  1420.       int j;
  1421.  
  1422.       /* Now unshare the structure beneath BASE_BINFO.  */
  1423.       for (j = TREE_VEC_LENGTH (base_binfos)-1;
  1424.            j >= 0; j--)
  1425.         {
  1426.           tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
  1427.           if (! TREE_VIA_VIRTUAL (base_base_binfo))
  1428.         TREE_VEC_ELT (base_binfos, j)
  1429.           = make_binfo (BINFO_OFFSET (base_base_binfo),
  1430.                 base_base_binfo,
  1431.                 BINFO_VTABLE (base_base_binfo),
  1432.                 BINFO_VIRTUALS (base_base_binfo),
  1433.                 chain);
  1434.           chain = TREE_VEC_ELT (base_binfos, j);
  1435.           TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
  1436.           TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
  1437.         }
  1438.  
  1439.       /* Completely unshare potentially shared data, and
  1440.          update what is ours.  */
  1441.       propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
  1442.     }
  1443.  
  1444.       if (! TREE_VIA_VIRTUAL (base_binfo))
  1445.     CLASSTYPE_N_SUPERCLASSES (t) += 1;
  1446.  
  1447.       if (TYPE_VIRTUAL_P (basetype))
  1448.     {
  1449.       /* If there's going to be a destructor needed, make
  1450.          sure it will be virtual.  */
  1451.       b->needs_virtual_dtor = 1;
  1452.  
  1453.       /* Don't borrow virtuals from virtual baseclasses.  */
  1454.       if (TREE_VIA_VIRTUAL (base_binfo))
  1455.         continue;
  1456.  
  1457.       if (first_vfn_base_index < 0)
  1458.         {
  1459.           tree vfields;
  1460.           first_vfn_base_index = i;
  1461.  
  1462.           /* Update these two, now that we know what vtable we are
  1463.          going to extend.  This is so that we can add virtual
  1464.          functions, and override them properly.  */
  1465.           BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
  1466.           BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
  1467.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1468.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1469.           b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
  1470.           vfields = b->vfields;
  1471.           while (vfields)
  1472.         {
  1473.           if (VF_BINFO_VALUE (vfields) == NULL_TREE
  1474.               || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
  1475.             {
  1476.               tree value = VF_BASETYPE_VALUE (vfields);
  1477.               if (DECL_NAME (CLASSTYPE_VFIELD (value))
  1478.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1479.             VF_NORMAL_VALUE (b->vfields) = basetype;
  1480.               else
  1481.             VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
  1482.             }
  1483.           vfields = TREE_CHAIN (vfields);
  1484.         }
  1485.           CLASSTYPE_VFIELD (t) = b->vfield;
  1486.         }
  1487.       else
  1488.         {
  1489.           /* Only add unique vfields, and flatten them out as we go.  */
  1490.           tree vfields = CLASSTYPE_VFIELDS (basetype);
  1491.           while (vfields)
  1492.         {
  1493.           if (VF_BINFO_VALUE (vfields) == NULL_TREE
  1494.               || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
  1495.             {
  1496.               tree value = VF_BASETYPE_VALUE (vfields);
  1497.               b->vfields = tree_cons (base_binfo, value, b->vfields);
  1498.               if (DECL_NAME (CLASSTYPE_VFIELD (value))
  1499.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1500.             VF_NORMAL_VALUE (b->vfields) = basetype;
  1501.               else
  1502.             VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
  1503.             }
  1504.           vfields = TREE_CHAIN (vfields);
  1505.         }
  1506.  
  1507.           if (b->has_virtual == 0)
  1508.         {
  1509.           first_vfn_base_index = i;
  1510.  
  1511.           /* Update these two, now that we know what vtable we are
  1512.              going to extend.  This is so that we can add virtual
  1513.              functions, and override them properly.  */
  1514.           BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
  1515.           BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
  1516.           b->has_virtual = CLASSTYPE_VSIZE (basetype);
  1517.           b->vfield = CLASSTYPE_VFIELD (basetype);
  1518.           CLASSTYPE_VFIELD (t) = b->vfield;
  1519.           /* When we install the first one, set the VF_NORMAL_VALUE
  1520.              to be the current class, as this it is the most derived
  1521.              class.  Hopefully, this is not set to something else
  1522.              later.  (mrs) */
  1523.           vfields = b->vfields;
  1524.           while (vfields)
  1525.             {
  1526.               if (DECL_NAME (CLASSTYPE_VFIELD (t))
  1527.               == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
  1528.             {
  1529.               VF_NORMAL_VALUE (vfields) = t;
  1530.               /* There should only be one of them!  And it should
  1531.                  always be found, if we get into here.  (mrs)  */
  1532.               break;
  1533.             }
  1534.               vfields = TREE_CHAIN (vfields);
  1535.             }
  1536.         }
  1537.         }
  1538.     }
  1539.     }
  1540.  
  1541.   /* Must come after offsets are fixed for all bases.  */
  1542.   for (i = 0; i < n_baseclasses; i++)
  1543.     {
  1544.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1545.       tree basetype = BINFO_TYPE (base_binfo);
  1546.  
  1547.       if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
  1548.     {
  1549.       cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
  1550.               basetype, t);
  1551.       b->cant_synth_asn_ref = 1;
  1552.       b->cant_synth_copy_ctor = 1;
  1553.     }
  1554.     }
  1555.   {
  1556.     tree v = get_vbase_types (t_binfo);
  1557.  
  1558.     for (; v; v = TREE_CHAIN (v))
  1559.       {
  1560.     tree basetype = BINFO_TYPE (v);
  1561.     if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
  1562.       {
  1563.         if (extra_warnings)
  1564.           cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
  1565.               basetype, t);
  1566.         b->cant_synth_asn_ref = 1;
  1567.         b->cant_synth_copy_ctor = 1;
  1568.       }
  1569.       }
  1570.   }    
  1571.  
  1572.   {
  1573.     tree vfields;
  1574.     /* Find the base class with the largest number of virtual functions.  */
  1575.     for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
  1576.       {
  1577.     if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
  1578.       b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
  1579.     if (VF_DERIVED_VALUE (vfields)
  1580.         && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
  1581.       b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
  1582.       }
  1583.   }
  1584.  
  1585.   if (b->vfield == 0)
  1586.     /* If all virtual functions come only from virtual baseclasses.  */
  1587.     return -1;
  1588.   return first_vfn_base_index;
  1589. }
  1590.  
  1591. static int
  1592. typecode_p (type, code)
  1593.      tree type;
  1594.      enum tree_code code;
  1595. {
  1596.   return (TREE_CODE (type) == code
  1597.       || (TREE_CODE (type) == REFERENCE_TYPE
  1598.           && TREE_CODE (TREE_TYPE (type)) == code));
  1599. }
  1600.  
  1601. /* Set memoizing fields and bits of T (and its variants) for later use.
  1602.    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
  1603. static void
  1604. finish_struct_bits (t, max_has_virtual)
  1605.      tree t;
  1606.      int max_has_virtual;
  1607. {
  1608.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1609.   tree method_vec = CLASSTYPE_METHOD_VEC (t);
  1610.  
  1611.   /* Fix up variants (if any).  */
  1612.   tree variants = TYPE_NEXT_VARIANT (t);
  1613.   while (variants)
  1614.     {
  1615.       /* These fields are in the _TYPE part of the node, not in
  1616.      the TYPE_LANG_SPECIFIC component, so they are not shared.  */
  1617.       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
  1618.       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
  1619.       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
  1620.       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
  1621.  
  1622.       TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
  1623.       TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
  1624.       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
  1625.       /* Copy whatever these are holding today.  */
  1626.       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
  1627.       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
  1628.       variants = TYPE_NEXT_VARIANT (variants);
  1629.     }
  1630.  
  1631.   if (n_baseclasses && max_has_virtual)
  1632.     {
  1633.       /* Done by `finish_struct' for classes without baseclasses.  */
  1634.       int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
  1635.       tree binfos = TYPE_BINFO_BASETYPES (t);
  1636.       for (i = n_baseclasses-1; i >= 0; i--)
  1637.     {
  1638.       might_have_abstract_virtuals
  1639.         |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
  1640.       if (might_have_abstract_virtuals)
  1641.         break;
  1642.     }
  1643.       if (might_have_abstract_virtuals)
  1644.     {
  1645.       /* We use error_mark_node from override_one_vtable to signal
  1646.          an artificial abstract. */
  1647.       if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
  1648.         CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
  1649.       CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
  1650.     }
  1651.     }
  1652.  
  1653.   if (n_baseclasses)
  1654.     {
  1655.       /* Notice whether this class has type conversion functions defined.  */
  1656.       tree binfo = TYPE_BINFO (t);
  1657.       tree binfos = BINFO_BASETYPES (binfo);
  1658.       tree basetype;
  1659.  
  1660.       for (i = n_baseclasses-1; i >= 0; i--)
  1661.     {
  1662.       basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
  1663.  
  1664.       if (TYPE_HAS_CONVERSION (basetype))
  1665.         {
  1666.           TYPE_HAS_CONVERSION (t) = 1;
  1667.           TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
  1668.           TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
  1669.         }
  1670.       if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
  1671.         CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
  1672.     }
  1673.     }
  1674.  
  1675.   /* Need to test METHOD_VEC here in case all methods
  1676.      (conversions and otherwise) are inherited.  */
  1677.   if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
  1678.     {
  1679.       tree first_conversions[last_conversion_type];
  1680.       tree last_conversions[last_conversion_type];
  1681.       enum conversion_type conv_index;
  1682.       tree *tmp;
  1683.       int i;
  1684.  
  1685.       bzero (first_conversions, sizeof (first_conversions));
  1686.       bzero (last_conversions, sizeof (last_conversions));
  1687.       for (tmp = &TREE_VEC_ELT (method_vec, 1);
  1688.        tmp != TREE_VEC_END (method_vec); tmp += 1)
  1689.     {
  1690.       /* ??? This should compare DECL_NAME (*tmp) == ansi_opname[TYPE_EXPR].  */
  1691.       if (IDENTIFIER_TYPENAME_P (DECL_ASSEMBLER_NAME (*tmp)))
  1692.         {
  1693.           tree fntype = TREE_TYPE (*tmp);
  1694.           tree return_type = TREE_TYPE (fntype);
  1695.           my_friendly_assert (TREE_CODE (fntype) == METHOD_TYPE, 171);
  1696.  
  1697.           if (typecode_p (return_type, POINTER_TYPE))
  1698.         {
  1699.           if (TYPE_READONLY (TREE_TYPE (return_type)))
  1700.             conv_index = constptr_conv;
  1701.           else
  1702.             conv_index = ptr_conv;
  1703.         }
  1704.           else if (typecode_p (return_type, INTEGER_TYPE)
  1705.                || typecode_p (return_type, BOOLEAN_TYPE)
  1706.                || typecode_p (return_type, ENUMERAL_TYPE))
  1707.         {
  1708.           TYPE_HAS_INT_CONVERSION (t) = 1;
  1709.           conv_index = int_conv;
  1710.         }
  1711.           else if (typecode_p (return_type, REAL_TYPE))
  1712.         {
  1713.           TYPE_HAS_REAL_CONVERSION (t) = 1;
  1714.           conv_index = real_conv;
  1715.         }
  1716.           else
  1717.         continue;
  1718.  
  1719.           if (first_conversions[(int) conv_index] == NULL_TREE)
  1720.         first_conversions[(int) conv_index] = *tmp;
  1721.           last_conversions[(int) conv_index] = *tmp;
  1722.         }
  1723.     }
  1724.  
  1725.       for (i = 0; i < (int) last_conversion_type; i++)
  1726.     if (first_conversions[i] != last_conversions[i])
  1727.       CLASSTYPE_CONVERSION (t, i) = error_mark_node;
  1728.     else
  1729.       CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
  1730.     }
  1731.  
  1732.   /* If this type has constructors, force its mode to be BLKmode,
  1733.      and force its TREE_ADDRESSABLE bit to be nonzero.  */
  1734.   if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
  1735.     {
  1736.       tree variants = t;
  1737.  
  1738.       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  1739.     DECL_MODE (TYPE_NAME (t)) = BLKmode;
  1740.       while (variants)
  1741.     {
  1742.       TYPE_MODE (variants) = BLKmode;
  1743.       TREE_ADDRESSABLE (variants) = 1;
  1744.       variants = TYPE_NEXT_VARIANT (variants);
  1745.     }
  1746.     }
  1747. }
  1748.  
  1749. /* Warn about duplicate methods in fn_fields.  Also compact method
  1750.    lists so that lookup can be made faster.
  1751.  
  1752.    Algorithm: Outer loop builds lists by method name.  Inner loop
  1753.    checks for redundant method names within a list.
  1754.  
  1755.    Data Structure: List of method lists.  The outer list is a
  1756.    TREE_LIST, whose TREE_PURPOSE field is the field name and the
  1757.    TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.  Friends are
  1758.    chained in the same way as member functions, but they live in the
  1759.    TREE_TYPE field of the outer list.  That allows them to be quickly
  1760.    deleted, and requires no extra storage.
  1761.  
  1762.    If there are any constructors/destructors, they are moved to the
  1763.    front of the list.  This makes pushclass more efficient.
  1764.  
  1765.    We also link each field which has shares a name with its baseclass
  1766.    to the head of the list of fields for that base class.  This allows
  1767.    us to reduce search time in places like `build_method_call' to
  1768.    consider only reasonably likely functions.  */
  1769.  
  1770. static tree
  1771. finish_struct_methods (t, fn_fields, nonprivate_method)
  1772.      tree t;
  1773.      tree fn_fields;
  1774.      int nonprivate_method;
  1775. {
  1776.   tree method_vec;
  1777.   tree name = constructor_name (t);
  1778.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  1779.  
  1780.   /* Now prepare to gather fn_fields into vector.  */
  1781.   struct obstack *ambient_obstack = current_obstack;
  1782.   current_obstack = &class_obstack;
  1783.   method_vec = make_node (TREE_VEC);
  1784.   /* Room has been saved for constructors and destructors.  */
  1785.   current_obstack = ambient_obstack;
  1786.   /* Now make this a live vector.  */
  1787.   obstack_free (&class_obstack, method_vec);
  1788.   obstack_blank (&class_obstack, sizeof (struct tree_vec));
  1789.  
  1790.   while (fn_fields)
  1791.     {
  1792.       /* NEXT Pointer, TEST Pointer, and BASE Pointer.  */
  1793.       tree nextp, *testp;
  1794.       tree fn_name = DECL_NAME (fn_fields);
  1795.       if (fn_name == NULL_TREE)
  1796.     fn_name = name;
  1797.  
  1798.       nextp = TREE_CHAIN (fn_fields);
  1799.       TREE_CHAIN (fn_fields) = NULL_TREE;
  1800.  
  1801.       /* Clear out this flag.
  1802.  
  1803.      @@ Doug may figure out how to break
  1804.      @@ this with nested classes and friends.  */
  1805.       DECL_IN_AGGR_P (fn_fields) = 0;
  1806.  
  1807.       /* Note here that a copy ctor is private, so we don't dare generate
  1808.       a default copy constructor for a class that has a member
  1809.       of this type without making sure they have access to it.  */
  1810.       if (fn_name == name)
  1811.      {
  1812.        tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
  1813.        tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
  1814.       
  1815.        if (TREE_CODE (parmtype) == REFERENCE_TYPE
  1816.            && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
  1817.          {
  1818.            if (TREE_CHAIN (parmtypes) == NULL_TREE
  1819.            || TREE_CHAIN (parmtypes) == void_list_node
  1820.            || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
  1821.          {
  1822.            if (TREE_PROTECTED (fn_fields))
  1823.              TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
  1824.            else if (TREE_PRIVATE (fn_fields))
  1825.              TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
  1826.          }
  1827.          }
  1828.      }
  1829.       else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
  1830.     {
  1831.       tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
  1832.  
  1833.       if (TREE_CODE (parmtype) == REFERENCE_TYPE
  1834.           && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
  1835.         {
  1836.           if (TREE_PROTECTED (fn_fields))
  1837.         TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
  1838.           else if (TREE_PRIVATE (fn_fields))
  1839.         TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
  1840.         }
  1841.     }
  1842.  
  1843.       /* Constructors are handled easily in search routines.  */
  1844.       if (fn_name == name)
  1845.     {
  1846.       DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 0);
  1847.       TREE_VEC_ELT (method_vec, 0) = fn_fields;
  1848.     }
  1849.       else
  1850.     {
  1851.       testp = &TREE_VEC_ELT (method_vec, 0);
  1852.       if (*testp == NULL_TREE)
  1853.         testp++;
  1854.       while (((HOST_WIDE_INT) testp
  1855.           < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
  1856.          && DECL_NAME (*testp) != fn_name)
  1857.         testp++;
  1858.       if ((HOST_WIDE_INT) testp
  1859.           < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
  1860.         {
  1861.           tree x, prev_x;
  1862.  
  1863.           for (x = *testp; x; x = DECL_CHAIN (x))
  1864.         {
  1865.           if (DECL_NAME (fn_fields) == ansi_opname[(int) DELETE_EXPR]
  1866.               || DECL_NAME (fn_fields)
  1867.                  == ansi_opname[(int) VEC_DELETE_EXPR])
  1868.             {
  1869.               /* ANSI C++ June 5 1992 WP 12.5.5.1 */
  1870.               cp_error_at ("`%D' overloaded", fn_fields);
  1871.               cp_error_at ("previous declaration as `%D' here", x);
  1872.             }
  1873.           if (DECL_ASSEMBLER_NAME (fn_fields)==DECL_ASSEMBLER_NAME (x))
  1874.             {
  1875.               /* We complain about multiple destructors on sight,
  1876.              so we do not repeat the warning here.  Friend-friend
  1877.              ambiguities are warned about outside this loop.  */
  1878.               if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn_fields)))
  1879.             cp_error_at ("ambiguous method `%#D' in structure",
  1880.                      fn_fields);
  1881.               break;
  1882.             }
  1883.           prev_x = x;
  1884.         }
  1885.           if (x == 0)
  1886.         {
  1887.           if (*testp)
  1888.             DECL_CHAIN (prev_x) = fn_fields;
  1889.           else
  1890.             *testp = fn_fields;
  1891.         }
  1892.         }
  1893.       else
  1894.         {
  1895.           obstack_ptr_grow (&class_obstack, fn_fields);
  1896.           method_vec = (tree)obstack_base (&class_obstack);
  1897.         }
  1898.     }
  1899.       fn_fields = nextp;
  1900.     }
  1901.  
  1902.   TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
  1903.     - (&TREE_VEC_ELT (method_vec, 0));
  1904.   obstack_finish (&class_obstack);
  1905.   CLASSTYPE_METHOD_VEC (t) = method_vec;
  1906.  
  1907.   if (nonprivate_method == 0
  1908.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  1909.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  1910.     {
  1911.       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
  1912.       for (i = 0; i < n_baseclasses; i++)
  1913.     if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
  1914.         || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
  1915.       {
  1916.         nonprivate_method = 1;
  1917.         break;
  1918.       }
  1919.       if (nonprivate_method == 0)
  1920.     cp_warning ("all member functions in class `%T' are private", t);
  1921.     }
  1922.  
  1923.   /* If there are constructors (and destructors), they are at the
  1924.      front.  Place destructors at very front.  Also warn if all
  1925.      constructors and/or destructors are private (in which case this
  1926.      class is effectively unusable.  */
  1927.   if (TYPE_HAS_DESTRUCTOR (t))
  1928.     {
  1929.       tree dtor, prev;
  1930.  
  1931.       for (dtor = TREE_VEC_ELT (method_vec, 0);
  1932.        dtor;
  1933.        prev = dtor, dtor = DECL_CHAIN (dtor))
  1934.     {
  1935.       if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (dtor)))
  1936.         {
  1937.           if (TREE_PRIVATE (dtor)
  1938.           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  1939.           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE
  1940.           && warn_ctor_dtor_privacy)
  1941.         warning ("class `%s' only defines a private destructor and has no friends",
  1942.              TYPE_NAME_STRING (t));
  1943.           break;
  1944.         }
  1945.     }
  1946.  
  1947.       /* Wild parse errors can cause this to happen.  */
  1948.       if (dtor == NULL_TREE)
  1949.     TYPE_HAS_DESTRUCTOR (t) = 0;
  1950.       else if (dtor != TREE_VEC_ELT (method_vec, 0))
  1951.     {
  1952.       DECL_CHAIN (prev) = DECL_CHAIN (dtor);
  1953.       DECL_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
  1954.       TREE_VEC_ELT (method_vec, 0) = dtor;
  1955.     }
  1956.     }
  1957.  
  1958.   /* Now for each member function (except for constructors and
  1959.      destructors), compute where member functions of the same
  1960.      name reside in base classes.  */
  1961.   if (n_baseclasses != 0
  1962.       && TREE_VEC_LENGTH (method_vec) > 1)
  1963.     {
  1964.       int len = TREE_VEC_LENGTH (method_vec);
  1965.       tree baselink_vec = make_tree_vec (len);
  1966.       int any_links = 0;
  1967.       tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
  1968.  
  1969.       for (i = 1; i < len; i++)
  1970.     {
  1971.       TREE_VEC_ELT (baselink_vec, i)
  1972.         = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
  1973.       if (TREE_VEC_ELT (baselink_vec, i) != 0)
  1974.         any_links = 1;
  1975.     }
  1976.       if (any_links != 0)
  1977.     CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
  1978.       else
  1979.     obstack_free (current_obstack, baselink_vec);
  1980.     }
  1981.  
  1982.   /* Now add the methods to the TYPE_METHODS of T, arranged in a chain.  */
  1983.   {
  1984.     tree x, last_x = NULL_TREE;
  1985.     int limit = TREE_VEC_LENGTH (method_vec);
  1986.  
  1987.     for (i = 1; i < limit; i++)
  1988.       {
  1989.     for (x = TREE_VEC_ELT (method_vec, i); x; x = DECL_CHAIN (x))
  1990.       {
  1991.         if (last_x != NULL_TREE)
  1992.           TREE_CHAIN (last_x) = x;
  1993.         last_x = x;
  1994.       }
  1995.       }
  1996.  
  1997.     /* Put ctors and dtors at the front of the list.  */
  1998.     x = TREE_VEC_ELT (method_vec, 0);
  1999.     if (x)
  2000.       {
  2001.     while (DECL_CHAIN (x))
  2002.       {
  2003.         /* Let's avoid being circular about this.  */
  2004.         if (x == DECL_CHAIN (x))
  2005.           break;
  2006.         TREE_CHAIN (x) = DECL_CHAIN (x);
  2007.         x = DECL_CHAIN (x);
  2008.       }
  2009.     if (TREE_VEC_LENGTH (method_vec) > 1)
  2010.       TREE_CHAIN (x) = TREE_VEC_ELT (method_vec, 1);
  2011.     else
  2012.       TREE_CHAIN (x) = NULL_TREE;
  2013.       }
  2014.   }
  2015.  
  2016.   TYPE_METHODS (t) = method_vec;
  2017.  
  2018.   return method_vec;
  2019. }
  2020.  
  2021. /* Emit error when a duplicate definition of a type is seen.  Patch up. */
  2022.  
  2023. void
  2024. duplicate_tag_error (t)
  2025.      tree t;
  2026. {
  2027.   cp_error ("redefinition of `%#T'", t);
  2028.  
  2029.   /* Pretend we haven't defined this type.  */
  2030.  
  2031.   /* All of the component_decl's were TREE_CHAINed together in the parser.
  2032.      finish_struct_methods walks these chains and assembles all methods with
  2033.      the same base name into DECL_CHAINs. Now we don't need the parser chains
  2034.      anymore, so we unravel them.
  2035.    */
  2036.   /*
  2037.    * This used to be in finish_struct, but it turns out that the
  2038.    * TREE_CHAIN is used by dbxout_type_methods and perhaps some other things...
  2039.    */
  2040.   if (CLASSTYPE_METHOD_VEC(t)) 
  2041.     {
  2042.       tree tv = CLASSTYPE_METHOD_VEC(t);
  2043.       int i, len  = TREE_VEC_LENGTH (tv);
  2044.       for (i = 0; i < len; i++)
  2045.     {
  2046.       tree unchain = TREE_VEC_ELT (tv, i);
  2047.       while (unchain != NULL_TREE) 
  2048.         {
  2049.           TREE_CHAIN (unchain) = NULL_TREE;
  2050.           unchain = DECL_CHAIN(unchain);
  2051.         }
  2052.     }
  2053.     }
  2054.  
  2055.   if (TYPE_LANG_SPECIFIC (t))
  2056.     {
  2057.       tree as_list = CLASSTYPE_AS_LIST (t);
  2058.       tree binfo = TYPE_BINFO (t);
  2059.       tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
  2060.       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
  2061.       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
  2062.  
  2063.       bzero (TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
  2064.       BINFO_BASETYPES(binfo) = NULL_TREE;
  2065.  
  2066.       CLASSTYPE_AS_LIST (t) = as_list;
  2067.       TYPE_BINFO (t) = binfo;
  2068.       CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
  2069.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2070.       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
  2071.       CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
  2072.       TYPE_REDEFINED (t) = 1;
  2073.     }
  2074.   TYPE_SIZE (t) = NULL_TREE;
  2075.   TYPE_MODE (t) = VOIDmode;
  2076.   TYPE_FIELDS (t) = NULL_TREE;
  2077.   TYPE_METHODS (t) = NULL_TREE;
  2078.   TYPE_VFIELD (t) = NULL_TREE;
  2079.   TYPE_CONTEXT (t) = NULL_TREE;
  2080. }
  2081.  
  2082. /* finish up all new vtables. */
  2083. static void
  2084. finish_vtbls (binfo, do_self, t)
  2085.      tree binfo, t;
  2086.      int do_self;
  2087. {
  2088.   tree binfos = BINFO_BASETYPES (binfo);
  2089.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2090.  
  2091.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2092.   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2093.     {
  2094.       if (BINFO_NEW_VTABLE_MARKED (binfo))
  2095.     {
  2096.       tree decl, context;
  2097.  
  2098.       decl = BINFO_VTABLE (binfo);
  2099.       context = DECL_CONTEXT (decl);
  2100.       DECL_CONTEXT (decl) = 0;
  2101.       if (write_virtuals >= 0
  2102.           && DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
  2103.         DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
  2104.                         BINFO_VIRTUALS (binfo));
  2105.       finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
  2106.       DECL_CONTEXT (decl) = context;
  2107.     }
  2108.       CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
  2109.     }
  2110.  
  2111.   for (i = 0; i < n_baselinks; i++)
  2112.     {
  2113.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2114.       int is_not_base_vtable =
  2115.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2116.       if (TREE_VIA_VIRTUAL (base_binfo))
  2117.     {
  2118.       base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
  2119.     }
  2120.       finish_vtbls (base_binfo, is_not_base_vtable, t);
  2121.     }
  2122. }
  2123.  
  2124. /* True if we should override the given BASE_FNDECL with the given
  2125.    FNDECL.  */
  2126. static int
  2127. overrides (fndecl, base_fndecl)
  2128.      tree fndecl, base_fndecl;
  2129. {
  2130.   /* Destructors have special names. */
  2131.   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) &&
  2132.       DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
  2133.     return 1;
  2134.   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) ||
  2135.       DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
  2136.     return 0;
  2137.   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
  2138.     {
  2139.       tree rettype, base_rettype, types, base_types;
  2140. #if 0
  2141.       retypes = TREE_TYPE (TREE_TYPE (fndecl));
  2142.       base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
  2143. #endif
  2144.       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  2145.       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
  2146.       if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (base_types)))
  2147.        == TYPE_READONLY (TREE_TYPE (TREE_VALUE (types))))
  2148.       && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types), 3))
  2149.     return 1;
  2150.     }
  2151.   return 0;
  2152. }
  2153.  
  2154. static void
  2155. modify_one_vtable (binfo, t, fndecl, pfn)
  2156.      tree binfo, t, fndecl, pfn;
  2157. {
  2158.   tree virtuals = BINFO_VIRTUALS (binfo);
  2159.   unsigned HOST_WIDE_INT n;
  2160.   
  2161.   n = 0;
  2162.   /* Skip initial vtable length field and RTTI fake object. */
  2163.   for (; virtuals && n < 1 + flag_dossier; n++)
  2164.       virtuals = TREE_CHAIN (virtuals);
  2165.   while (virtuals)
  2166.     {
  2167.       tree current_fndecl = TREE_VALUE (virtuals);
  2168.       current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
  2169.       current_fndecl = TREE_OPERAND (current_fndecl, 0);
  2170.       if (current_fndecl && overrides (fndecl, current_fndecl))
  2171.     {
  2172.       tree base_offset, offset;
  2173.       tree context = DECL_CLASS_CONTEXT (fndecl);
  2174.       tree vfield = CLASSTYPE_VFIELD (t);
  2175.       tree this_offset;
  2176.  
  2177.       offset = integer_zero_node;
  2178.       if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
  2179.         {
  2180.           offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
  2181.           if (offset == NULL_TREE)
  2182.         {
  2183.           tree binfo = get_binfo (context, t, 0);
  2184.           offset = BINFO_OFFSET (binfo);
  2185.         }
  2186.         }
  2187.  
  2188.       /* Find the right offset for the this pointer based on the
  2189.          base class we just found.  We have to take into
  2190.          consideration the virtual base class pointers that we
  2191.          stick in before the virtual function table pointer.
  2192.  
  2193.          Also, we want just the delta bewteen the most base class
  2194.          that we derived this vfield from and us.  */
  2195.       base_offset = size_binop (PLUS_EXPR,
  2196.                     get_derived_offset (binfo),
  2197.                     BINFO_OFFSET (binfo));
  2198.       this_offset = size_binop (MINUS_EXPR, offset, base_offset);
  2199.  
  2200.       /* Make sure we can modify the derived association with immunity.  */
  2201.       if (TREE_USED (binfo)) {
  2202.         my_friendly_assert (0, 999);
  2203. #if 0
  2204.         my_friendly_assert (*binfo2_ptr == binfo, 999);
  2205.         *binfo2_ptr = copy_binfo (binfo);
  2206. #endif
  2207.       }
  2208.       if (binfo == TYPE_BINFO (t))
  2209.         {
  2210.           /* In this case, it is *type*'s vtable we are modifying.
  2211.          We start with the approximation that it's vtable is that
  2212.          of the immediate base class.  */
  2213.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2214.         build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
  2215.         }
  2216.       else
  2217.         {
  2218.           /* This is our very own copy of `basetype' to play with.
  2219.          Later, we will fill in all the virtual functions
  2220.          that override the virtual functions in these base classes
  2221.          which are not defined by the current type.  */
  2222.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2223.         prepare_fresh_vtable (binfo, t);
  2224.         }
  2225.  
  2226. #ifdef NOTQUITE
  2227.       cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
  2228. #endif
  2229.       modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
  2230.                    build_vtable_entry (this_offset, pfn),
  2231.                    fndecl);
  2232.     }
  2233.       ++n;
  2234.       virtuals = TREE_CHAIN (virtuals);
  2235.     }
  2236. }
  2237.  
  2238. /* These are the ones that are not through virtual base classes. */
  2239. static void
  2240. modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
  2241.      tree binfo, t, fndecl, pfn;
  2242.      int do_self;
  2243. {
  2244.   tree binfos = BINFO_BASETYPES (binfo);
  2245.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2246.  
  2247.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2248.   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2249.     {
  2250.       modify_one_vtable (binfo, t, fndecl, pfn);
  2251.     }
  2252.  
  2253.   for (i = 0; i < n_baselinks; i++)
  2254.     {
  2255.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2256.       int is_not_base_vtable =
  2257.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2258.       if (! TREE_VIA_VIRTUAL (base_binfo))
  2259.     modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
  2260.     }
  2261. }
  2262.  
  2263. /* Fixup all the delta entries in this vtable that need updating.
  2264.    This happens when we have non-overridden virtual functions from a
  2265.    virtual base class, that are at a different offset, in the new
  2266.    hierarchy, because the layout of the virtual bases has changed.  */
  2267. static void
  2268. fixup_vtable_deltas (binfo, t)
  2269.      tree binfo, t;
  2270. {
  2271.   tree virtuals = BINFO_VIRTUALS (binfo);
  2272.   unsigned HOST_WIDE_INT n;
  2273.   
  2274.   n = 0;
  2275.   /* Skip initial vtable length field and RTTI fake object. */
  2276.   for (; virtuals && n < 1 + flag_dossier; n++)
  2277.       virtuals = TREE_CHAIN (virtuals);
  2278.   while (virtuals)
  2279.     {
  2280.       tree fndecl = TREE_VALUE (virtuals);
  2281.       tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
  2282.       tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
  2283.       fndecl = TREE_OPERAND (pfn, 0);
  2284.       if (fndecl)
  2285.     {
  2286.       tree base_offset, offset;
  2287.       tree context = DECL_CLASS_CONTEXT (fndecl);
  2288.       tree vfield = CLASSTYPE_VFIELD (t);
  2289.       tree this_offset;
  2290.  
  2291.       offset = integer_zero_node;
  2292.       if (context != t && TYPE_USES_COMPLEX_INHERITANCE (t))
  2293.         {
  2294.           offset = virtual_offset (context, CLASSTYPE_VBASECLASSES (t), offset);
  2295.           if (offset == NULL_TREE)
  2296.         {
  2297.           tree binfo = get_binfo (context, t, 0);
  2298.           offset = BINFO_OFFSET (binfo);
  2299.         }
  2300.         }
  2301.  
  2302.       /* Find the right offset for the this pointer based on the
  2303.          base class we just found.  We have to take into
  2304.          consideration the virtual base class pointers that we
  2305.          stick in before the virtual function table pointer.
  2306.  
  2307.          Also, we want just the delta bewteen the most base class
  2308.          that we derived this vfield from and us.  */
  2309.       base_offset = size_binop (PLUS_EXPR,
  2310.                     get_derived_offset (binfo),
  2311.                     BINFO_OFFSET (binfo));
  2312.       this_offset = size_binop (MINUS_EXPR, offset, base_offset);
  2313.  
  2314.       if (! tree_int_cst_equal (this_offset, delta))
  2315.         {
  2316.           /* Make sure we can modify the derived association with immunity.  */
  2317.           if (TREE_USED (binfo))
  2318.         my_friendly_assert (0, 999);
  2319.  
  2320.           if (binfo == TYPE_BINFO (t))
  2321.         {
  2322.           /* In this case, it is *type*'s vtable we are modifying.
  2323.              We start with the approximation that it's vtable is that
  2324.              of the immediate base class.  */
  2325.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2326.             build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
  2327.         }
  2328.           else
  2329.         {
  2330.           /* This is our very own copy of `basetype' to play with.
  2331.              Later, we will fill in all the virtual functions
  2332.              that override the virtual functions in these base classes
  2333.              which are not defined by the current type.  */
  2334.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2335.             prepare_fresh_vtable (binfo, t);
  2336.         }
  2337.  
  2338.           modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
  2339.                    build_vtable_entry (this_offset, pfn),
  2340.                    fndecl);
  2341.         }
  2342.     }
  2343.       ++n;
  2344.       virtuals = TREE_CHAIN (virtuals);
  2345.     }
  2346. }
  2347.  
  2348. /* These are the ones that are through virtual base classes. */
  2349. static void
  2350. modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
  2351.      tree binfo, t, fndecl, pfn;
  2352.      int do_self, via_virtual;
  2353. {
  2354.   tree binfos = BINFO_BASETYPES (binfo);
  2355.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2356.  
  2357.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2358.   if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2359.     {
  2360.       modify_one_vtable (binfo, t, fndecl, pfn);
  2361.     }
  2362.  
  2363.   for (i = 0; i < n_baselinks; i++)
  2364.     {
  2365.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2366.       int is_not_base_vtable =
  2367.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2368.       if (TREE_VIA_VIRTUAL (base_binfo))
  2369.     {
  2370.       via_virtual = 1;
  2371.       base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
  2372.     }
  2373.       modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
  2374.     }
  2375. }
  2376.  
  2377. static void
  2378. modify_all_vtables (t, fndecl, vfn)
  2379.      tree t, fndecl, vfn;
  2380. {
  2381.   /* Do these first, so that we will make use of any non-virtual class's
  2382.      vtable, over a virtual classes vtable. */
  2383.   modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
  2384.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  2385.     modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
  2386. }
  2387.  
  2388. /* Here, we already know that they match in every respect.
  2389.    All we have to check is where they had their declarations.  */
  2390. static int 
  2391. strictly_overrides (fndecl1, fndecl2)
  2392.      tree fndecl1, fndecl2;
  2393. {
  2394.   int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
  2395.                     DECL_CLASS_CONTEXT (fndecl1),
  2396.                     0, (tree *)0);
  2397.   if (distance == -2 || distance > 0)
  2398.     return 1;
  2399.   return 0;
  2400. }
  2401.  
  2402. /* Merge overrides for one vtable.
  2403.    If we want to merge in same function, we are fine.
  2404.    else
  2405.      if one has a DECL_CLASS_CONTEXT that is a parent of the
  2406.        other, than choose the more derived one
  2407.      else
  2408.        potentially ill-formed (see 10.3 [class.virtual])
  2409.        we have to check later to see if there was an
  2410.        override in this class.  If there was ok, if not
  2411.        then it is ill-formed.  (mrs)
  2412.  
  2413.    We take special care to reuse a vtable, if we can.  */
  2414. static void
  2415. override_one_vtable (binfo, old, t)
  2416.      tree binfo, old, t;
  2417. {
  2418.   tree virtuals = BINFO_VIRTUALS (binfo);
  2419.   tree old_virtuals = BINFO_VIRTUALS (old);
  2420.   enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
  2421.  
  2422.   /* If we have already committed to modifying it, then don't try and
  2423.      reuse another vtable. */
  2424.   if (BINFO_NEW_VTABLE_MARKED (binfo))
  2425.     choose = NEITHER;
  2426.  
  2427.   /* Skip size entry. */
  2428.   virtuals = TREE_CHAIN (virtuals);
  2429.   /* Skip RTTI fake object. */
  2430.   if (flag_dossier)
  2431.     {
  2432.       virtuals = TREE_CHAIN (virtuals);
  2433.     }
  2434.  
  2435.   /* Skip size entry. */
  2436.   old_virtuals = TREE_CHAIN (old_virtuals);
  2437.   /* Skip RTTI fake object. */
  2438.   if (flag_dossier)
  2439.     {
  2440.       old_virtuals = TREE_CHAIN (old_virtuals);
  2441.     }
  2442.  
  2443.   while (virtuals)
  2444.     {
  2445.       tree fndecl = TREE_VALUE (virtuals);
  2446.       tree old_fndecl = TREE_VALUE (old_virtuals);
  2447.       fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
  2448.       old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
  2449.       fndecl = TREE_OPERAND (fndecl, 0);
  2450.       old_fndecl = TREE_OPERAND (old_fndecl, 0);
  2451.       /* First check to see if they are the same. */
  2452.       if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
  2453.     {
  2454.       /* No need to do anything. */
  2455.     }
  2456.       else if (strictly_overrides (fndecl, old_fndecl))
  2457.     {
  2458.       if (choose == UNDECIDED)
  2459.         choose = REUSE_NEW;
  2460.       else if (choose == REUSE_OLD)
  2461.         {
  2462.           choose = NEITHER;
  2463.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2464.         {
  2465.           prepare_fresh_vtable (binfo, t);
  2466.           override_one_vtable (binfo, old, t);
  2467.           return;
  2468.         }
  2469.         }
  2470.     }
  2471.       else if (strictly_overrides (old_fndecl, fndecl))
  2472.     {
  2473.       if (choose == UNDECIDED)
  2474.         choose = REUSE_OLD;
  2475.       else if (choose == REUSE_NEW)
  2476.         {
  2477.           choose = NEITHER;
  2478.           if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2479.         {
  2480.           prepare_fresh_vtable (binfo, t);
  2481.           override_one_vtable (binfo, old, t);
  2482.           return;
  2483.         }
  2484.           TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
  2485.         }
  2486.       else if (choose == NEITHER)
  2487.         {
  2488.           TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
  2489.         }  
  2490.     }
  2491.       else
  2492.     {
  2493.       choose = NEITHER;
  2494.       if (! BINFO_NEW_VTABLE_MARKED (binfo))
  2495.         {
  2496.           prepare_fresh_vtable (binfo, t);
  2497.           override_one_vtable (binfo, old, t);
  2498.           return;
  2499.         }
  2500.       {
  2501.         /* This MUST be overriden, or the class is ill-formed.  */
  2502.         /* For now, we just make it abstract.  */
  2503.         tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  2504.         tree vfn;
  2505.  
  2506.         fndecl = copy_node (fndecl);
  2507.         copy_lang_decl (fndecl);
  2508.         DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
  2509.         /* Make sure we search for it later. */
  2510.         if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
  2511.           CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
  2512.  
  2513.         vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
  2514.         TREE_CONSTANT (vfn) = 1;
  2515.         
  2516.         /* We can use integer_zero_node, as we will will core dump
  2517.            if this is used anyway. */
  2518.         TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
  2519.       }
  2520.     }
  2521.       virtuals = TREE_CHAIN (virtuals);
  2522.       old_virtuals = TREE_CHAIN (old_virtuals);
  2523.     }
  2524.  
  2525.   /* Let's reuse the old vtable. */
  2526.   if (choose == REUSE_OLD)
  2527.     {
  2528.       BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
  2529.       BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
  2530.     }
  2531. }
  2532.  
  2533. /* Merge in overrides for virtual bases.
  2534.    BINFO is the hierarchy we want to modify, and OLD has the potential
  2535.    overrides.  */
  2536. static void
  2537. merge_overrides (binfo, old, do_self, t)
  2538.      tree binfo, old, t;
  2539.      int do_self;
  2540. {
  2541.   tree binfos = BINFO_BASETYPES (binfo);
  2542.   tree old_binfos = BINFO_BASETYPES (old);
  2543.   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  2544.  
  2545.   /* Should we use something besides CLASSTYPE_VFIELDS? */
  2546.   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
  2547.     {
  2548.       override_one_vtable (binfo, old, t);
  2549.     }
  2550.  
  2551.   for (i = 0; i < n_baselinks; i++)
  2552.     {
  2553.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  2554.       tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
  2555.       int is_not_base_vtable =
  2556.     i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
  2557.       if (! TREE_VIA_VIRTUAL (base_binfo))
  2558.     merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
  2559.     }
  2560. }
  2561.  
  2562. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
  2563.    (or C++ class declaration).
  2564.  
  2565.    For C++, we must handle the building of derived classes.
  2566.    Also, C++ allows static class members.  The way that this is
  2567.    handled is to keep the field name where it is (as the DECL_NAME
  2568.    of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
  2569.    of the field.  layout_record and layout_union will know about this.
  2570.  
  2571.    More C++ hair: inline functions have text in their
  2572.    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
  2573.    meaningful tree structure.  After the struct has been laid out, set
  2574.    things up so that this can happen.
  2575.  
  2576.    And still more: virtual functions.  In the case of single inheritance,
  2577.    when a new virtual function is seen which redefines a virtual function
  2578.    from the base class, the new virtual function is placed into
  2579.    the virtual function table at exactly the same address that
  2580.    it had in the base class.  When this is extended to multiple
  2581.    inheritance, the same thing happens, except that multiple virtual
  2582.    function tables must be maintained.  The first virtual function
  2583.    table is treated in exactly the same way as in the case of single
  2584.    inheritance.  Additional virtual function tables have different
  2585.    DELTAs, which tell how to adjust `this' to point to the right thing.
  2586.  
  2587.    LIST_OF_FIELDLISTS is just that.  The elements of the list are
  2588.    TREE_LIST elements, whose TREE_PURPOSE field tells what access
  2589.    the list has, and the TREE_VALUE slot gives the actual fields.
  2590.  
  2591.    If flag_all_virtual == 1, then we lay all functions into
  2592.    the virtual function table, as though they were declared
  2593.    virtual.  Constructors do not lay down in the virtual function table.
  2594.  
  2595.    If flag_all_virtual == 2, then we lay all functions into
  2596.    the virtual function table, such that virtual functions
  2597.    occupy a space by themselves, and then all functions
  2598.    of the class occupy a space by themselves.  This is illustrated
  2599.    in the following diagram:
  2600.  
  2601.    class A; class B : A;
  2602.  
  2603.     Class A's vtbl:            Class B's vtbl:
  2604.     --------------------------------------------------------------------
  2605.    | A's virtual functions|        | B's virtual functions        |
  2606.    |              |        | (may inherit some from A).    |
  2607.     --------------------------------------------------------------------
  2608.    | All of A's functions |        | All of A's functions        |
  2609.    | (such as a->A::f).      |        | (such as b->A::f)        |
  2610.     --------------------------------------------------------------------
  2611.                     | B's new virtual functions    |
  2612.                     | (not defined in A.)        |
  2613.                      -------------------------------
  2614.                     | All of B's functions        |
  2615.                     | (such as b->B::f)        |
  2616.                      -------------------------------
  2617.  
  2618.    this allows the program to make references to any function, virtual
  2619.    or otherwise in a type-consistent manner.  */
  2620.  
  2621. tree
  2622. finish_struct (t, list_of_fieldlists, warn_anon)
  2623.      tree t;
  2624.      tree list_of_fieldlists;
  2625.      int warn_anon;
  2626. {
  2627.   extern int interface_only, interface_unknown;
  2628.  
  2629.   int old;
  2630.   int round_up_size = 1;
  2631.  
  2632.   enum tree_code code = TREE_CODE (t);
  2633.   register tree x, last_x, method_vec;
  2634.   int needs_virtual_dtor;
  2635.   tree name = TYPE_NAME (t), fields, fn_fields, *tail;
  2636.   tree *tail_user_methods = &CLASSTYPE_METHODS (t);
  2637.   enum access_type access;
  2638.   int all_virtual;
  2639.   int has_virtual;
  2640.   int max_has_virtual;
  2641.   tree pending_virtuals = NULL_TREE;
  2642.   tree abstract_virtuals = NULL_TREE;
  2643.   tree vfield;
  2644.   tree vfields;
  2645.   int cant_have_default_ctor;
  2646.   int cant_have_const_ctor;
  2647.   int cant_synth_copy_ctor;
  2648.   int cant_synth_asn_ref;
  2649.   int no_const_asn_ref;
  2650.  
  2651.   /* The index of the first base class which has virtual
  2652.      functions.  Only applied to non-virtual baseclasses.  */
  2653.   int first_vfn_base_index;
  2654.  
  2655.   int n_baseclasses;
  2656.   int any_default_members = 0;
  2657.   int const_sans_init = 0;
  2658.   int ref_sans_init = 0;
  2659.   int nonprivate_method = 0;
  2660.   tree t_binfo = TYPE_BINFO (t);
  2661.   tree access_decls = NULL_TREE;
  2662.  
  2663.   if (TREE_CODE (name) == TYPE_DECL)
  2664.     {
  2665. #if 0                /* Maybe later.  -jason  */
  2666.       struct tinst_level *til = tinst_for_decl();
  2667.  
  2668.       if (til)
  2669.     {
  2670.       DECL_SOURCE_FILE (name) = til->file;
  2671.       if (DECL_SOURCE_LINE (name))
  2672.         DECL_SOURCE_LINE (name) = til->line;
  2673.     }
  2674.       else
  2675. #endif
  2676.     {
  2677.       extern int lineno;
  2678.       
  2679.       DECL_SOURCE_FILE (name) = input_filename;
  2680.       /* For TYPE_DECL that are not typedefs (those marked with a line
  2681.          number of zero, we don't want to mark them as real typedefs.
  2682.          If this fails one needs to make sure real typedefs have a
  2683.          previous line number, even if it is wrong, that way the below
  2684.          will fill in the right line number.  (mrs) */
  2685.       if (DECL_SOURCE_LINE (name))
  2686.         DECL_SOURCE_LINE (name) = lineno;
  2687.     }
  2688.       name = DECL_NAME (name);
  2689.     }
  2690.  
  2691.   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
  2692.     pedwarn ("anonymous class type not used to declare any objects");
  2693.  
  2694.   if (TYPE_SIZE (t))
  2695.     {
  2696.       if (IS_AGGR_TYPE (t))
  2697.     cp_error ("redefinition of `%#T'", t);
  2698.       else
  2699.     my_friendly_abort (172);
  2700.       popclass (0);
  2701.       return t;
  2702.     }
  2703.  
  2704.   /* Append the fields we need for constructing signature tables.  */
  2705.   if (IS_SIGNATURE (t))
  2706.     append_signature_fields (list_of_fieldlists);
  2707.  
  2708.   GNU_xref_decl (current_function_decl, t);
  2709.  
  2710.   /* If this type was previously laid out as a forward reference,
  2711.      make sure we lay it out again.  */
  2712.  
  2713.   TYPE_SIZE (t) = NULL_TREE;
  2714.   CLASSTYPE_GOT_SEMICOLON (t) = 0;
  2715.  
  2716.   /* A signature type will contain the fields of the signature table.
  2717.      Therefore, it's not only an interface.  */
  2718.   if (IS_SIGNATURE (t))
  2719.     {
  2720.       CLASSTYPE_INTERFACE_ONLY (t) = 0;
  2721.       SET_CLASSTYPE_INTERFACE_KNOWN (t);
  2722.     }
  2723.   else
  2724.     {
  2725.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2726.       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
  2727.     }
  2728.  
  2729.   if (flag_dossier)
  2730.     build_t_desc (t, 0);
  2731.  
  2732.   TYPE_BINFO (t) = NULL_TREE;
  2733.  
  2734.   old = suspend_momentary ();
  2735.  
  2736.   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
  2737.      Also process specified field sizes.
  2738.      Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
  2739.      The specified size is found in the DECL_INITIAL.
  2740.      Store 0 there, except for ": 0" fields (so we can find them
  2741.      and delete them, below).  */
  2742.  
  2743.   if (t_binfo && BINFO_BASETYPES (t_binfo))
  2744.     n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
  2745.   else
  2746.     n_baseclasses = 0;
  2747.  
  2748.   if (n_baseclasses > 0)
  2749.     {
  2750.       struct base_info base_info;
  2751.  
  2752.       /* If using multiple inheritance, this may cause variants of our
  2753.      basetypes to be used (instead of their canonical forms).  */
  2754.       fields = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
  2755.       last_x = tree_last (fields);
  2756.  
  2757.       first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo);
  2758.       /* Remember where we got our vfield from */
  2759.       CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
  2760.       has_virtual = base_info.has_virtual;
  2761.       max_has_virtual = base_info.max_has_virtual;
  2762.       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
  2763.       vfield = base_info.vfield;
  2764.       vfields = base_info.vfields;
  2765.       cant_have_default_ctor = base_info.cant_have_default_ctor;
  2766.       cant_have_const_ctor = base_info.cant_have_const_ctor;
  2767.       cant_synth_copy_ctor = base_info.cant_synth_copy_ctor;
  2768.       cant_synth_asn_ref = base_info.cant_synth_asn_ref;
  2769.       no_const_asn_ref = base_info.no_const_asn_ref;
  2770.       needs_virtual_dtor = base_info.needs_virtual_dtor;
  2771.       n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
  2772.     }
  2773.   else
  2774.     {
  2775.       first_vfn_base_index = -1;
  2776.       has_virtual = 0;
  2777.       max_has_virtual = has_virtual;
  2778.       vfield = NULL_TREE;
  2779.       vfields = NULL_TREE;
  2780.       fields = NULL_TREE;
  2781.       last_x = NULL_TREE;
  2782.       cant_have_default_ctor = 0;
  2783.       cant_have_const_ctor = 0;
  2784.       cant_synth_copy_ctor = 0;
  2785.       cant_synth_asn_ref = 0;
  2786.       no_const_asn_ref = 0;
  2787.       needs_virtual_dtor = 0;
  2788.     }
  2789.  
  2790.   if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
  2791.       && ! IS_SIGNATURE (t))
  2792.     {
  2793.       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
  2794.       CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
  2795.     }
  2796.   else if (IS_SIGNATURE (t))
  2797.     CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 0;
  2798.  
  2799.   /* The three of these are approximations which may later be
  2800.      modified.  Needed at this point to make add_virtual_function
  2801.      and modify_vtable_entries work.  */
  2802.   TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
  2803.   TYPE_BINFO (t) = t_binfo;
  2804.   CLASSTYPE_VFIELDS (t) = vfields;
  2805.   CLASSTYPE_VFIELD (t) = vfield;
  2806.  
  2807.   tail = &fn_fields;
  2808.   if (last_x && list_of_fieldlists)
  2809.     TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
  2810.  
  2811.   if (IS_SIGNATURE (t))
  2812.     all_virtual = 0;
  2813.   else if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
  2814.     all_virtual = 1;
  2815.   else
  2816.     all_virtual = 0;
  2817.  
  2818.   /* For signatures, we made all methods `public' in the parser and
  2819.      reported an error if a access specifier was used.  */
  2820.   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
  2821.     {
  2822.       nonprivate_method = 1;
  2823.       if (list_of_fieldlists
  2824.       && TREE_PURPOSE (list_of_fieldlists) == (tree)access_default)
  2825.     TREE_PURPOSE (list_of_fieldlists) = (tree)access_public;
  2826.     }
  2827.   else if (list_of_fieldlists
  2828.        && TREE_PURPOSE (list_of_fieldlists) == (tree)access_default)
  2829.     TREE_PURPOSE (list_of_fieldlists) = (tree)access_private;
  2830.  
  2831.   while (list_of_fieldlists)
  2832.     {
  2833.       access = (enum access_type)TREE_PURPOSE (list_of_fieldlists);
  2834.  
  2835.       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
  2836.     {
  2837.       TREE_PRIVATE (x) = access == access_private;
  2838.       TREE_PROTECTED (x) = access == access_protected;
  2839.       GNU_xref_member (current_class_name, x);
  2840.  
  2841.           if (TREE_CODE (x) == TYPE_DECL)
  2842.             {
  2843.           /* Make sure we set this up.  In find_scoped_type, it explicitly
  2844.          looks for a TYPE_DECL in the TYPE_FIELDS list.  If we don't
  2845.          do this here, we'll miss including this TYPE_DECL in the
  2846.          list.  */
  2847.           if (! fields)
  2848.         fields = x;
  2849.           last_x = x;
  2850.           continue;
  2851.         }
  2852.  
  2853.       /* Check for inconsistent use of this name in the class body.
  2854.              Enums, types and static vars have already been checked.  */
  2855.       if (TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
  2856.         {
  2857.           tree name = DECL_NAME (x);
  2858.           tree icv;
  2859.  
  2860.           /* Don't get confused by access decls.  */
  2861.           if (name && TREE_CODE (name) == IDENTIFIER_NODE)
  2862.         icv = IDENTIFIER_CLASS_VALUE (name);
  2863.           else
  2864.         icv = NULL_TREE;
  2865.  
  2866.           if (icv
  2867.           /* Don't complain about constructors.  */
  2868.           && name != constructor_name (current_class_type)
  2869.           /* Or inherited names.  */
  2870.           && id_in_current_class (name)
  2871.           /* Or shadowed tags.  */
  2872.           && !(TREE_CODE (icv) == TYPE_DECL
  2873.                && DECL_CONTEXT (icv) == t))
  2874.         {
  2875.           cp_error_at ("declaration of identifier `%D' as `%+#D'",
  2876.                    name, x);
  2877.           cp_error_at ("conflicts with other use in class as `%#D'",
  2878.                    icv);
  2879.         }
  2880.         }
  2881.  
  2882.       if (TREE_CODE (x) == FUNCTION_DECL)
  2883.         {
  2884.           nonprivate_method |= ! TREE_PRIVATE (x);
  2885.  
  2886.           /* If this was an evil function, don't keep it in class.  */
  2887.           if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
  2888.         continue;
  2889.  
  2890.           if (last_x)
  2891.         TREE_CHAIN (last_x) = TREE_CHAIN (x);
  2892.           /* Link x onto end of fn_fields and CLASSTYPE_METHODS. */
  2893.           *tail = x;
  2894.           tail = &TREE_CHAIN (x);
  2895.           *tail_user_methods = x;
  2896.           tail_user_methods = &DECL_NEXT_METHOD (x);
  2897.  
  2898.           DECL_CLASS_CONTEXT (x) = t;
  2899.  
  2900.           DECL_FIELD_SIZE (x) = 0;
  2901.  
  2902.           /* The name of the field is the original field name
  2903.          Save this in auxiliary field for later overloading.  */
  2904.           if (DECL_VINDEX (x)
  2905.           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
  2906.         {
  2907.                   pending_virtuals = add_virtual_function (pending_virtuals,
  2908.                                                            &has_virtual, x, t);
  2909.                   if (DECL_ABSTRACT_VIRTUAL_P (x))
  2910.                     abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
  2911.         }
  2912.           continue;
  2913.         }
  2914.  
  2915.       /* Handle access declarations.  */
  2916.       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
  2917.         {
  2918.           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
  2919.  
  2920.           if (last_x)
  2921.         TREE_CHAIN (last_x) = TREE_CHAIN (x);
  2922.           access_decls = tree_cons ((tree) access, fdecl, access_decls);
  2923.           continue;
  2924.         }
  2925.  
  2926.       /* If we've gotten this far, it's a data member, possibly static,
  2927.          or an enumerator. */
  2928.  
  2929.       DECL_FIELD_CONTEXT (x) = t;
  2930.  
  2931.       /* ``A local class cannot have static data members.'' ARM 9.4 */
  2932.       if (current_function_decl && TREE_STATIC (x))
  2933.         cp_error_at ("field `%D' in local class cannot be static", x);
  2934.  
  2935.       /* Perform error checking that did not get done in
  2936.              grokdeclarator.  */
  2937.       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
  2938.         {
  2939.           cp_error_at ("field `%D' invalidly declared function type",
  2940.             x);
  2941.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2942.         }
  2943.       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
  2944.         {
  2945.           cp_error_at ("field `%D' invalidly declared method type", x);
  2946.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2947.         }
  2948.       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
  2949.         {
  2950.           cp_error_at ("field `%D' invalidly declared offset type", x);
  2951.           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
  2952.         }
  2953.  
  2954.       if (TREE_TYPE (x) == error_mark_node)
  2955.         continue;
  2956.       
  2957.       if (! fields)
  2958.         fields = x;
  2959.       last_x = x;
  2960.  
  2961.       DECL_FIELD_SIZE (x) = 0;
  2962.  
  2963.       /* When this goes into scope, it will be a non-local reference.  */
  2964.       DECL_NONLOCAL (x) = 1;
  2965.  
  2966.       if (TREE_CODE (x) == CONST_DECL)
  2967.         continue;
  2968.  
  2969.       if (TREE_CODE (x) == VAR_DECL)
  2970.         {
  2971.           if (TREE_CODE (t) == UNION_TYPE)
  2972.         /* Unions cannot have static members.  */
  2973.         cp_error_at ("field `%D' declared static in union", x);
  2974.           
  2975.           continue;
  2976.         }
  2977.  
  2978.       /* Now it can only be a FIELD_DECL.  */
  2979.  
  2980.       /* If this is of reference type, check if it needs an init.
  2981.          Also do a little ANSI jig if necessary.  */
  2982.       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
  2983.         {
  2984.           if (DECL_INITIAL (x) == NULL_TREE)
  2985.         ref_sans_init = 1;
  2986.  
  2987.           /* ARM $12.6.2: [A member initializer list] (or, for an
  2988.          aggregate, initialization by a brace-enclosed list) is the
  2989.          only way to initialize nonstatic const and reference
  2990.          members.  */
  2991.           cant_synth_asn_ref = 1;
  2992.           cant_have_default_ctor = 1;
  2993.           TYPE_HAS_COMPLEX_INIT_REF (t) = 1;
  2994.  
  2995.           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
  2996.         {
  2997.           if (DECL_NAME (x))
  2998.             cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
  2999.           else
  3000.             cp_warning_at ("non-static reference in class without a constructor", x);
  3001.         }
  3002.         }
  3003.  
  3004.       /* If any field is const, the structure type is pseudo-const.  */
  3005.       if (TREE_READONLY (x))
  3006.         {
  3007.           C_TYPE_FIELDS_READONLY (t) = 1;
  3008.           if (DECL_INITIAL (x) == NULL_TREE)
  3009.         const_sans_init = 1;
  3010.  
  3011.           /* ARM $12.6.2: [A member initializer list] (or, for an
  3012.          aggregate, initialization by a brace-enclosed list) is the
  3013.          only way to initialize nonstatic const and reference
  3014.          members.  */
  3015.           cant_synth_asn_ref = 1;
  3016.           cant_have_default_ctor = 1;
  3017.           TYPE_HAS_COMPLEX_INIT_REF (t) = 1;
  3018.  
  3019.           if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
  3020.           && extra_warnings)
  3021.         {
  3022.           if (DECL_NAME (x))
  3023.             cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
  3024.           else
  3025.             cp_warning_at ("non-static const member in class without a constructor", x);
  3026.         }
  3027.         }
  3028.       else
  3029.         {
  3030.           /* A field that is pseudo-const makes the structure
  3031.          likewise.  */
  3032.           tree t1 = TREE_TYPE (x);
  3033.           while (TREE_CODE (t1) == ARRAY_TYPE)
  3034.         t1 = TREE_TYPE (t1);
  3035.           if (IS_AGGR_TYPE (t1))
  3036.         {
  3037.           if (C_TYPE_FIELDS_READONLY (t1))
  3038.             C_TYPE_FIELDS_READONLY (t) = 1;
  3039.           if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
  3040.             const_sans_init = 1;
  3041.         }
  3042.         }
  3043.  
  3044.       /* We set DECL_BIT_FIELD tentatively in grokbitfield.
  3045.          If the type and width are valid, we'll keep it set.
  3046.          Otherwise, the flag is cleared.  */
  3047.       if (DECL_BIT_FIELD (x))
  3048.         {
  3049.           DECL_BIT_FIELD (x) = 0;
  3050.           /* Invalid bit-field size done by grokfield.  */
  3051.           /* Detect invalid bit-field type.  */
  3052.           if (DECL_INITIAL (x)
  3053.           && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
  3054.         {
  3055.           cp_error_at ("bit-field `%#D' with non-integral type", x);
  3056.           DECL_INITIAL (x) = NULL;
  3057.         }
  3058.  
  3059.           /* Detect and ignore out of range field width.  */
  3060.           if (DECL_INITIAL (x))
  3061.         {
  3062.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  3063.  
  3064.           if (width < 0)
  3065.             {
  3066.               DECL_INITIAL (x) = NULL;
  3067.               cp_error_at ("negative width in bit-field `%D'", x);
  3068.             }
  3069.           else if (width == 0 && DECL_NAME (x) != 0)
  3070.             {
  3071.               DECL_INITIAL (x) = NULL;
  3072.               cp_error_at ("zero width for bit-field `%D'", x);
  3073.             }
  3074.           else if ((unsigned)width > TYPE_PRECISION (TREE_TYPE (x)))
  3075.             {
  3076.               DECL_INITIAL (x) = NULL;
  3077.               cp_error_at ("width of `%D' exceeds its type", x);
  3078.             }
  3079.         }
  3080.  
  3081.           /* Process valid field width.  */
  3082.           if (DECL_INITIAL (x))
  3083.         {
  3084.           register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  3085.  
  3086.           if (width == 0)
  3087.             {
  3088. #ifdef EMPTY_FIELD_BOUNDARY
  3089.               /* field size 0 => mark following field as "aligned" */
  3090.               if (TREE_CHAIN (x))
  3091.             DECL_ALIGN (TREE_CHAIN (x))
  3092.               = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
  3093.               /* field of size 0 at the end => round up the size.  */
  3094.               else
  3095.             round_up_size = EMPTY_FIELD_BOUNDARY;
  3096. #endif
  3097. #ifdef PCC_BITFIELD_TYPE_MATTERS
  3098.               DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
  3099.                         TYPE_ALIGN (TREE_TYPE (x)));
  3100. #endif
  3101.             }
  3102.           else
  3103.             {
  3104.               DECL_INITIAL (x) = NULL_TREE;
  3105.               DECL_FIELD_SIZE (x) = width;
  3106.               DECL_BIT_FIELD (x) = 1;
  3107.               /* Traditionally a bit field is unsigned
  3108.              even if declared signed.  */
  3109.               if (flag_traditional
  3110.               && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  3111.             TREE_TYPE (x) = unsigned_type_node;
  3112.             }
  3113.         }
  3114.           else
  3115.         /* Non-bit-fields are aligned for their type.  */
  3116.         DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
  3117.         }
  3118.       else
  3119.         {
  3120.           tree type = TREE_TYPE (x);
  3121.  
  3122.           if (TREE_CODE (type) == ARRAY_TYPE)
  3123.         type = TREE_TYPE (type);
  3124.  
  3125.           if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
  3126.           && ! TYPE_PTRMEMFUNC_P (type))
  3127.         {
  3128.           /* Never let anything with uninheritable virtuals
  3129.              make it through without complaint.  */
  3130.           if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
  3131.             abstract_virtuals_error (x, type);
  3132.               
  3133.           /* Don't let signatures make it through either.  */
  3134.           if (IS_SIGNATURE (type))
  3135.             signature_error (x, type);
  3136.               
  3137.           if (code == UNION_TYPE)
  3138.             {
  3139.               char *fie = NULL;
  3140.               if (TYPE_NEEDS_CONSTRUCTING (type))
  3141.             fie = "constructor";
  3142.               else if (TYPE_NEEDS_DESTRUCTOR (type))
  3143.             fie = "destructor";
  3144.               else if (TYPE_HAS_REAL_ASSIGNMENT (type))
  3145.             fie = "assignment operator";
  3146.               if (fie)
  3147.             cp_error_at ("member `%#D' with %s not allowed in union", x,
  3148.                      fie);
  3149.             }
  3150.           else
  3151.             {
  3152.               TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
  3153.               TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
  3154.               TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
  3155.               TYPE_HAS_COMPLEX_INIT_REF (t)
  3156.             |= (TYPE_HAS_COMPLEX_INIT_REF (type)
  3157.                 || TYPE_NEEDS_CONSTRUCTING (type));
  3158.             }
  3159.  
  3160.           if (! TYPE_HAS_INIT_REF (type)
  3161.               || (TYPE_HAS_NONPUBLIC_CTOR (type)
  3162.               && ! is_friend (t, type)))
  3163.             cant_synth_copy_ctor = 1;
  3164.           else if (!TYPE_HAS_CONST_INIT_REF (type))
  3165.             cant_have_const_ctor = 1;
  3166.  
  3167.           if (! TYPE_HAS_ASSIGN_REF (type)
  3168.               || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (type)
  3169.               && ! is_friend (t, type)))
  3170.             cant_synth_asn_ref = 1;
  3171.           else if (!TYPE_HAS_CONST_ASSIGN_REF (type))
  3172.             no_const_asn_ref = 1;
  3173.  
  3174.           if (TYPE_HAS_CONSTRUCTOR (type)
  3175.               && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
  3176.             {
  3177.               cant_have_default_ctor = 1;
  3178.               if (! TYPE_HAS_CONSTRUCTOR (t))
  3179.             {
  3180.               if (DECL_NAME (x))
  3181.                 cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
  3182.               else
  3183.                 cp_pedwarn_at ("member with only non-default constructor", x);
  3184.               cp_pedwarn_at ("in class without a constructor",
  3185.                      x);
  3186.             }
  3187.             }
  3188.         }
  3189.           if (DECL_INITIAL (x) != NULL_TREE)
  3190.         {
  3191.           /* `build_class_init_list' does not recognize
  3192.                      non-FIELD_DECLs.  */
  3193.           if (code == UNION_TYPE && any_default_members != 0)
  3194.             cp_error_at ("multiple fields in union `%T' initialized");
  3195.           any_default_members = 1;
  3196.         }
  3197.         }
  3198.     }
  3199.       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
  3200.       /* link the tail while we have it! */
  3201.       if (last_x)
  3202.     {
  3203.       TREE_CHAIN (last_x) = NULL_TREE;
  3204.  
  3205.       if (list_of_fieldlists
  3206.           && TREE_VALUE (list_of_fieldlists)
  3207.           && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
  3208.         TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
  3209.     }
  3210.     }
  3211.  
  3212.   /* If this type has any constant members which did not come
  3213.      with their own initialization, mark that fact here.  It is
  3214.      not an error here, since such types can be saved either by their
  3215.      constructors, or by fortuitous initialization.  */
  3216.   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
  3217.   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
  3218.   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
  3219.  
  3220.   if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
  3221.       && !IS_SIGNATURE (t))
  3222.     {
  3223.       /* Here we must cons up a destructor on the fly.  */
  3224.       tree dtor = cons_up_default_function (t, name, fields,
  3225.                         needs_virtual_dtor != 0);
  3226.  
  3227.       /* If we couldn't make it work, then pretend we didn't need it.  */
  3228.       if (dtor == void_type_node)
  3229.     TYPE_NEEDS_DESTRUCTOR (t) = 0;
  3230.       else
  3231.     {
  3232.       /* Link dtor onto end of fn_fields. */
  3233.       *tail = dtor;
  3234.       tail = &TREE_CHAIN (dtor);
  3235.  
  3236.       if (DECL_VINDEX (dtor) == NULL_TREE
  3237.           && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  3238.           && (needs_virtual_dtor
  3239.           || pending_virtuals != NULL_TREE
  3240.           || pending_hard_virtuals != NULL_TREE))
  3241.         DECL_VINDEX (dtor) = error_mark_node;
  3242.       if (DECL_VINDEX (dtor))
  3243.         pending_virtuals = add_virtual_function (pending_virtuals,
  3244.                              &has_virtual, dtor, t);
  3245.       nonprivate_method = 1;
  3246.     }
  3247.     }
  3248.  
  3249.   *tail = NULL_TREE;
  3250.   *tail_user_methods = NULL_TREE;
  3251.  
  3252.   TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
  3253.  
  3254.   /* Synthesize any needed methods.  Note that methods will be synthesized
  3255.      for anonymous unions; grok_x_components undoes that.  */
  3256.  
  3257.   if (! fn_fields)
  3258.     nonprivate_method = 1;
  3259.  
  3260.   TYPE_HAS_COMPLEX_INIT_REF (t)
  3261.     |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
  3262.     || has_virtual || any_default_members || first_vfn_base_index >= 0);
  3263.   TYPE_NEEDS_CONSTRUCTING (t)
  3264.     |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
  3265.     || has_virtual || any_default_members || first_vfn_base_index >= 0);
  3266.  
  3267.   /* ARM $12.1: A default constructor will be generated for a class X
  3268.      only if no constructor has been declared for class X.  So we
  3269.      check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
  3270.      one if they declared a constructor in this class.  */
  3271.   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
  3272.       && ! IS_SIGNATURE (t))
  3273.     {
  3274.       tree default_fn = cons_up_default_function (t, name, fields, 2);
  3275.       TREE_CHAIN (default_fn) = fn_fields;
  3276.       fn_fields = default_fn;
  3277.     }
  3278.  
  3279.   /* Create default copy constructor, if needed.  */
  3280.   if (! TYPE_HAS_INIT_REF (t) && ! cant_synth_copy_ctor
  3281.       && ! IS_SIGNATURE (t))
  3282.     {
  3283.       /* ARM 12.18: You get either X(X&) or X(const X&), but
  3284.      not both.  --Chip  */
  3285.       tree default_fn =
  3286.     cons_up_default_function (t, name, fields,
  3287.                   cant_have_const_ctor ? 4 : 3);
  3288.       TREE_CHAIN (default_fn) = fn_fields;
  3289.       fn_fields = default_fn;
  3290.     }
  3291.  
  3292.   TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
  3293.   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
  3294.   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
  3295.     |= (TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
  3296.     || has_virtual || first_vfn_base_index >= 0);
  3297.  
  3298.   if (! TYPE_HAS_ASSIGN_REF (t) && ! cant_synth_asn_ref
  3299.       && ! IS_SIGNATURE (t))
  3300.     {
  3301.       tree default_fn =
  3302.     cons_up_default_function (t, name, fields,
  3303.                   no_const_asn_ref ? 6 : 5);
  3304.       TREE_CHAIN (default_fn) = fn_fields;
  3305.       fn_fields = default_fn;
  3306.     }
  3307.  
  3308.   if (fn_fields)
  3309.     {
  3310.       method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
  3311.  
  3312.       if (TYPE_HAS_CONSTRUCTOR (t)
  3313.       && ! CLASSTYPE_DECLARED_EXCEPTION (t)
  3314.       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
  3315.       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
  3316.     {
  3317.       int nonprivate_ctor = 0;
  3318.       tree ctor;
  3319.  
  3320.       for (ctor = TREE_VEC_ELT (method_vec, 0);
  3321.            ctor;
  3322.            ctor = DECL_CHAIN (ctor))
  3323.         if (! TREE_PRIVATE (ctor))
  3324.           {
  3325.         nonprivate_ctor = 1;
  3326.         break;
  3327.           }
  3328.  
  3329.       if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
  3330.         cp_warning ("`%#T' only defines private constructors and has no friends",
  3331.             t);
  3332.     }
  3333.     }
  3334.   else
  3335.     {
  3336.       method_vec = 0;
  3337.  
  3338.       /* Just in case these got accidentally
  3339.      filled in by syntax errors.  */
  3340.       TYPE_HAS_CONSTRUCTOR (t) = 0;
  3341.       TYPE_HAS_DESTRUCTOR (t) = 0;
  3342.     }
  3343.  
  3344.   {
  3345.     int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
  3346.     
  3347.     for (access_decls = nreverse (access_decls); access_decls;
  3348.      access_decls = TREE_CHAIN (access_decls))
  3349.       {
  3350.     tree fdecl = TREE_VALUE (access_decls);
  3351.     tree flist = NULL_TREE;
  3352.     tree name;
  3353.     enum access_type access = (enum access_type)TREE_PURPOSE(access_decls);
  3354.     int i = 0;
  3355.     tree tmp;
  3356.  
  3357.     if (TREE_CODE (fdecl) == TREE_LIST)
  3358.       {
  3359.         flist = fdecl;
  3360.         fdecl = TREE_VALUE (flist);
  3361.       }
  3362.  
  3363.     name = DECL_NAME (fdecl);
  3364.  
  3365.     for (; i < n_methods; i++)
  3366.       if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
  3367.         {
  3368.           cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
  3369.           cp_error_at ("  because of local method `%#D' with same name",
  3370.                TREE_VEC_ELT (method_vec, i));
  3371.           fdecl = NULL_TREE;
  3372.           break;
  3373.         }
  3374.  
  3375.     if (! fdecl)
  3376.       continue;
  3377.     
  3378.     for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
  3379.       if (DECL_NAME (tmp) == name)
  3380.         {
  3381.           cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
  3382.           cp_error_at ("  because of local field `%#D' with same name", tmp);
  3383.           fdecl = NULL_TREE;
  3384.           break;
  3385.         }
  3386.  
  3387.     if (!fdecl)
  3388.       continue;
  3389.     
  3390.     /* Make type T see field decl FDECL with access ACCESS.*/
  3391.     if (flist)
  3392.       {
  3393.         fdecl = TREE_VALUE (flist);
  3394.         while (fdecl)
  3395.           {
  3396.         if (alter_access (t, fdecl, access) == 0)
  3397.           break;
  3398.         fdecl = DECL_CHAIN (fdecl);
  3399.           }
  3400.       }
  3401.     else
  3402.       alter_access (t, fdecl, access);
  3403.       }
  3404.     
  3405.   }
  3406.  
  3407.   if (vfield == NULL_TREE && has_virtual)
  3408.     {
  3409.       /* We build this decl with ptr_type_node, and
  3410.      change the type when we know what it should be.  */
  3411.       vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
  3412.                       ptr_type_node);
  3413.       /* If you change any of the below, take a look at all the
  3414.      other VFIELD_BASEs and VTABLE_BASEs in the code, and change
  3415.      them too. */
  3416.       DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
  3417.       CLASSTYPE_VFIELD (t) = vfield;
  3418.       DECL_VIRTUAL_P (vfield) = 1;
  3419.       DECL_FIELD_CONTEXT (vfield) = t;
  3420.       DECL_CLASS_CONTEXT (vfield) = t;
  3421.       DECL_FCONTEXT (vfield) = t;
  3422.       DECL_FIELD_SIZE (vfield) = 0;
  3423.       DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
  3424.       if (CLASSTYPE_DOSSIER (t))
  3425.     {
  3426.       /* vfield is always first entry in structure.  */
  3427.       TREE_CHAIN (vfield) = fields;
  3428.       fields = vfield;
  3429.     }
  3430.       else if (last_x)
  3431.     {
  3432.       my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
  3433.       TREE_CHAIN (last_x) = vfield;
  3434.       last_x = vfield;
  3435.     }
  3436.       else
  3437.     fields = vfield;
  3438.       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
  3439.     }
  3440.  
  3441.   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  3442.      And they have already done their work.
  3443.  
  3444.      C++: maybe we will support default field initialization some day...  */
  3445.  
  3446.   /* Delete all zero-width bit-fields from the front of the fieldlist */
  3447.   while (fields && DECL_BIT_FIELD (fields)
  3448.      && DECL_INITIAL (fields))
  3449.     fields = TREE_CHAIN (fields);
  3450.   /* Delete all such fields from the rest of the fields.  */
  3451.   for (x = fields; x;)
  3452.     {
  3453.       if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
  3454.       && DECL_INITIAL (TREE_CHAIN (x)))
  3455.     TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  3456.       else
  3457.     x = TREE_CHAIN (x);
  3458.     }
  3459.   /* Delete all duplicate fields from the fields */
  3460.   delete_duplicate_fields (fields);
  3461.  
  3462.   /* Now we have the final fieldlist for the data fields.  Record it,
  3463.      then lay out the structure or union (including the fields).  */
  3464.  
  3465.   TYPE_FIELDS (t) = fields;
  3466.  
  3467.   /* If there's a :0 field at the end, round the size to the
  3468.      EMPTY_FIELD_BOUNDARY.  */
  3469.   TYPE_ALIGN (t) = round_up_size;
  3470.  
  3471.   /* Pass layout information about base classes to layout_type, if any.  */
  3472.  
  3473.   {
  3474.     tree field;
  3475.     for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
  3476.       {
  3477.     if (TREE_STATIC (field))
  3478.       continue;
  3479.     if (TREE_CODE (field) != FIELD_DECL)
  3480.       continue;
  3481.  
  3482.     /* If this field is an anonymous union,
  3483.        give each union-member the same position as the union has.
  3484.  
  3485.        ??? This is a real kludge because it makes the structure
  3486.        of the types look strange.  This feature is only used by
  3487.        C++, which should have build_component_ref build two
  3488.        COMPONENT_REF operations, one for the union and one for
  3489.        the inner field.  We set the offset of this field to zero
  3490.        so that either the old or the correct method will work.
  3491.        Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
  3492.        moved into the type of this field, but nothing seems to break
  3493.        by doing this.  */
  3494.  
  3495.     if (DECL_NAME (field) == NULL_TREE
  3496.         && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  3497.       {
  3498.         tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  3499.         for (; uelt; uelt = TREE_CHAIN (uelt))
  3500.           {
  3501.         DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  3502.         DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
  3503.           }
  3504.  
  3505.         DECL_FIELD_BITPOS (field) = integer_zero_node;
  3506.       }
  3507.       }
  3508.   }
  3509.  
  3510.   if (n_baseclasses)
  3511.     {
  3512.       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
  3513.  
  3514.       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
  3515.       TYPE_FIELDS (t) = base_layout_decl;
  3516.  
  3517.       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
  3518.       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
  3519.       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
  3520.       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
  3521.       /* Don't re-use old size. */
  3522.       DECL_SIZE (base_layout_decl) = NULL_TREE;
  3523.     }
  3524.  
  3525.   layout_type (t);
  3526.  
  3527.   {
  3528.     tree field;
  3529.     for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
  3530.       {
  3531.     if (TREE_STATIC (field))
  3532.       continue;
  3533.     if (TREE_CODE (field) != FIELD_DECL)
  3534.       continue;
  3535.  
  3536.     /* If this field is an anonymous union,
  3537.        give each union-member the same position as the union has.
  3538.  
  3539.        ??? This is a real kludge because it makes the structure
  3540.        of the types look strange.  This feature is only used by
  3541.        C++, which should have build_component_ref build two
  3542.        COMPONENT_REF operations, one for the union and one for
  3543.        the inner field.  We set the offset of this field to zero
  3544.        so that either the old or the correct method will work.
  3545.        Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
  3546.        moved into the type of this field, but nothing seems to break
  3547.        by doing this.  */
  3548.  
  3549.     if (DECL_NAME (field) == NULL_TREE
  3550.         && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  3551.       {
  3552.         tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  3553.         for (; uelt; uelt = TREE_CHAIN (uelt))
  3554.           {
  3555.         DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  3556.         DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
  3557.           }
  3558.  
  3559.         DECL_FIELD_BITPOS (field) = integer_zero_node;
  3560.       }
  3561.       }
  3562.   }
  3563.  
  3564.   if (n_baseclasses)
  3565.     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
  3566.  
  3567.   /* C++: do not let empty structures exist.  */
  3568.   if (integer_zerop (TYPE_SIZE (t)))
  3569.     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
  3570.  
  3571.   /* Set the TYPE_DECL for this type to contain the right
  3572.      value for DECL_OFFSET, so that we can use it as part
  3573.      of a COMPONENT_REF for multiple inheritance.  */
  3574.  
  3575.   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
  3576.     layout_decl (TYPE_NAME (t), 0);
  3577.  
  3578.   /* Now fix up any virtual base class types that we left lying
  3579.      around.  We must get these done before we try to lay out the
  3580.      virtual function table.  */
  3581.   doing_hard_virtuals = 1;
  3582.   pending_hard_virtuals = nreverse (pending_hard_virtuals);
  3583.  
  3584.   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
  3585.     {
  3586.       tree vbases;
  3587.  
  3588.       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
  3589.       vbases = CLASSTYPE_VBASECLASSES (t);
  3590.       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
  3591.  
  3592.       while (vbases)
  3593.     {
  3594.       /* The rtti code should do this.  (mrs) */
  3595.       /* Update dossier info with offsets for virtual baseclasses.  */
  3596.       if (flag_dossier && ! BINFO_NEW_VTABLE_MARKED (vbases))
  3597.         prepare_fresh_vtable (vbases, t);
  3598.       vbases = TREE_CHAIN (vbases);
  3599.     }
  3600.  
  3601.       {
  3602.     /* Now fixup overrides of all functions in vtables from all
  3603.        direct or indirect virtual base classes.  */
  3604.     tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
  3605.     int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  3606.  
  3607.     for (i = 0; i < n_baseclasses; i++)
  3608.       {
  3609.         tree base_binfo = TREE_VEC_ELT (binfos, i);
  3610.         tree basetype = BINFO_TYPE (base_binfo);
  3611.         tree vbases;
  3612.  
  3613.         vbases = CLASSTYPE_VBASECLASSES (basetype);
  3614.         while (vbases)
  3615.           {
  3616.         merge_overrides (binfo_member (BINFO_TYPE (vbases),
  3617.                            CLASSTYPE_VBASECLASSES (t)),
  3618.                  vbases, 1, t);
  3619.         vbases = TREE_CHAIN (vbases);
  3620.           }
  3621.       }
  3622.     }
  3623.  
  3624.       /* Now fixup any virtual function entries from virtual bases
  3625.      that have different deltas.  */
  3626.       vbases = CLASSTYPE_VBASECLASSES (t);
  3627.       while (vbases)
  3628.     {
  3629.       /* We might be able to shorten the ammount of work we do by
  3630.          only doing this for vtables that come from virtual bases
  3631.          that have differing offsets, but don't want to miss any
  3632.          entries.  */
  3633.       fixup_vtable_deltas (vbases, t);
  3634.       vbases = TREE_CHAIN (vbases);
  3635.     }
  3636.     }
  3637.  
  3638.   /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
  3639.      might need to know it for setting up the offsets in the vtable
  3640.      (or in thunks) below.  */
  3641.   if (vfield != NULL_TREE
  3642.       && DECL_FIELD_CONTEXT (vfield) != t)
  3643.     {
  3644.       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
  3645.       tree offset = BINFO_OFFSET (binfo);
  3646.  
  3647.       vfield = copy_node (vfield);
  3648.       copy_lang_decl (vfield);
  3649.  
  3650.       if (! integer_zerop (offset))
  3651.     offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
  3652.       DECL_FIELD_CONTEXT (vfield) = t;
  3653.       DECL_CLASS_CONTEXT (vfield) = t;
  3654.       DECL_FIELD_BITPOS (vfield)
  3655.     = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
  3656.       CLASSTYPE_VFIELD (t) = vfield;
  3657.     }
  3658.     
  3659. #ifdef NOTQUITE
  3660.   cp_warning ("Doing hard virtuals for %T...", t);
  3661. #endif
  3662.   while (pending_hard_virtuals)
  3663.     {
  3664.       modify_all_vtables (t,
  3665.               TREE_PURPOSE (pending_hard_virtuals),
  3666.               TREE_VALUE (pending_hard_virtuals));
  3667.       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
  3668.     }
  3669.   doing_hard_virtuals = 0;
  3670.  
  3671.   /* Under our model of GC, every C++ class gets its own virtual
  3672.      function table, at least virtually.  */
  3673.   if (pending_virtuals || CLASSTYPE_DOSSIER (t))
  3674.     {
  3675.       pending_virtuals = nreverse (pending_virtuals);
  3676.       /* We must enter these virtuals into the table.  */
  3677.       if (first_vfn_base_index < 0)
  3678.     {
  3679.       if (flag_dossier)
  3680.         pending_virtuals = tree_cons (NULL_TREE,
  3681.                       build_vtable_entry (integer_zero_node,
  3682.                                   build_t_desc (t, 0)),
  3683.                       pending_virtuals);
  3684.       pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
  3685.                     pending_virtuals);
  3686.       build_vtable (NULL_TREE, t);
  3687.     }
  3688.       else
  3689.     {
  3690.       /* Here we know enough to change the type of our virtual
  3691.          function table, but we will wait until later this function.  */
  3692.  
  3693.       if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
  3694.         build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
  3695.  
  3696.       /* Update the dossier pointer for this class.  */
  3697.       if (flag_dossier)
  3698.         TREE_VALUE (TREE_CHAIN (TYPE_BINFO_VIRTUALS (t)))
  3699.           = build_vtable_entry (integer_zero_node, build_t_desc (t, 0));
  3700.     }
  3701.  
  3702.       /* If this type has basetypes with constructors, then those
  3703.      constructors might clobber the virtual function table.  But
  3704.      they don't if the derived class shares the exact vtable of the base
  3705.      class.  */
  3706.  
  3707.       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  3708.     }
  3709.   else if (first_vfn_base_index >= 0)
  3710.     {
  3711.       tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
  3712.       /* This class contributes nothing new to the virtual function
  3713.      table.  However, it may have declared functions which
  3714.      went into the virtual function table "inherited" from the
  3715.      base class.  If so, we grab a copy of those updated functions,
  3716.      and pretend they are ours.  */
  3717.  
  3718.       /* See if we should steal the virtual info from base class.  */
  3719.       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
  3720.     TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
  3721.       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
  3722.     TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
  3723.       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
  3724.     CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
  3725.     }
  3726.  
  3727.   if (has_virtual > max_has_virtual)
  3728.     max_has_virtual = has_virtual;
  3729.   if (max_has_virtual || first_vfn_base_index >= 0)
  3730.     {
  3731.       TYPE_VIRTUAL_P (t) = 1;
  3732.       CLASSTYPE_VSIZE (t) = has_virtual;
  3733.       if (first_vfn_base_index >= 0)
  3734.     {
  3735.       if (pending_virtuals)
  3736.         TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
  3737.                         pending_virtuals);
  3738.     }
  3739.       else if (has_virtual)
  3740.     {
  3741.       TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
  3742.       if (write_virtuals >= 0)
  3743.         DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
  3744.     }
  3745.     }
  3746.  
  3747.   /* Now lay out the virtual function table.  */
  3748.   if (has_virtual)
  3749.     {
  3750.       tree atype, itype;
  3751.  
  3752.       if (TREE_TYPE (vfield) == ptr_type_node)
  3753.     {
  3754.       /* We must create a pointer to this table because
  3755.          the one inherited from base class does not exist.
  3756.          We will fill in the type when we know what it
  3757.          should really be.  Use `size_int' so values are memoized
  3758.          in common cases.  */
  3759.       itype = build_index_type (size_int (has_virtual));
  3760.       atype = build_array_type (vtable_entry_type, itype);
  3761.       layout_type (atype);
  3762.       TREE_TYPE (vfield) = build_pointer_type (atype);
  3763.     }
  3764.       else
  3765.     {
  3766.       atype = TREE_TYPE (TREE_TYPE (vfield));
  3767.  
  3768.       if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
  3769.         {
  3770.           /* We must extend (or create) the boundaries on this array,
  3771.          because we picked up virtual functions from multiple
  3772.          base classes.  */
  3773.           itype = build_index_type (size_int (has_virtual));
  3774.           atype = build_array_type (vtable_entry_type, itype);
  3775.           layout_type (atype);
  3776.           vfield = copy_node (vfield);
  3777.           TREE_TYPE (vfield) = build_pointer_type (atype);
  3778.         }
  3779.     }
  3780.  
  3781.       CLASSTYPE_VFIELD (t) = vfield;
  3782.       if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
  3783.     {
  3784.       TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
  3785.       layout_decl (TYPE_BINFO_VTABLE (t), 0);
  3786.       /* At one time the vtable info was grabbed 2 words at a time.  This
  3787.          fails on sparc unless you have 8-byte alignment.  (tiemann) */
  3788.       DECL_ALIGN (TYPE_BINFO_VTABLE (t))
  3789.         = MAX (TYPE_ALIGN (double_type_node),
  3790.            DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
  3791.     }
  3792.     }
  3793.   else if (first_vfn_base_index >= 0)
  3794.     CLASSTYPE_VFIELD (t) = vfield;
  3795.   CLASSTYPE_VFIELDS (t) = vfields;
  3796.  
  3797.   finish_struct_bits (t, max_has_virtual);
  3798.  
  3799.   /* Promote each bit-field's type to int if it is narrower than that.
  3800.      There's more: complete the rtl for any static member objects which
  3801.      is of the same type we're working on.  */
  3802.   for (x = fields; x; x = TREE_CHAIN (x))
  3803.     {
  3804.       if (DECL_BIT_FIELD (x)
  3805.       && (C_PROMOTING_INTEGER_TYPE_P (TREE_TYPE (x))
  3806.           || DECL_FIELD_SIZE (x) < TYPE_PRECISION (integer_type_node)))
  3807.     {
  3808.       tree type = TREE_TYPE (x);
  3809.  
  3810.       /* Preserve unsignedness if traditional or if not really getting
  3811.          any wider.  */
  3812.       if (TREE_UNSIGNED (type)
  3813.           && (flag_traditional
  3814.           ||
  3815.           (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
  3816.            && DECL_FIELD_SIZE (x) == TYPE_PRECISION (integer_type_node))))
  3817.         TREE_TYPE (x) = unsigned_type_node;
  3818.       else
  3819.         TREE_TYPE (x) = integer_type_node;
  3820.     }
  3821.  
  3822.       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
  3823.       && TREE_TYPE (x) == t)
  3824.     {
  3825.       DECL_MODE (x) = TYPE_MODE (t);
  3826.       make_decl_rtl (x, NULL, 0);
  3827.     }
  3828.     }
  3829.  
  3830.   /* Now add the tags, if any, to the list of TYPE_DECLs
  3831.      defined for this type.  */
  3832.   if (CLASSTYPE_TAGS (t))
  3833.     {
  3834.       x = CLASSTYPE_TAGS (t);
  3835.       last_x = tree_last (TYPE_FIELDS (t));
  3836.       while (x)
  3837.     {
  3838. #if 0 /* What's wrong with using the decl the type already has? */
  3839.       tree tag = build_decl (TYPE_DECL, TREE_PURPOSE (x), TREE_VALUE (x));
  3840.       DECL_CONTEXT (tag) = t;
  3841. #else
  3842.       tree tag = TYPE_NAME (TREE_VALUE (x));
  3843. #endif
  3844.  
  3845. #ifdef DWARF_DEBUGGING_INFO
  3846.       if (write_symbols == DWARF_DEBUG)
  3847.         {
  3848.           /* Notify dwarfout.c that this TYPE_DECL node represent a
  3849.          gratuitous typedef.  */
  3850.           DECL_IGNORED_P (tag) = 1;
  3851.         }
  3852. #endif /* DWARF_DEBUGGING_INFO */
  3853.  
  3854.       TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
  3855.       x = TREE_CHAIN (x);
  3856.       last_x = chainon (last_x, tag);
  3857.     }
  3858.       if (TYPE_FIELDS (t) == NULL_TREE)
  3859.     TYPE_FIELDS (t) = last_x;
  3860.       CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
  3861.     }
  3862.  
  3863.   if (TYPE_HAS_CONSTRUCTOR (t))
  3864.     {
  3865.       tree vfields = CLASSTYPE_VFIELDS (t);
  3866.  
  3867.       while (vfields)
  3868.     {
  3869.       /* Mark the fact that constructor for T
  3870.          could affect anybody inheriting from T
  3871.          who wants to initialize vtables for VFIELDS's type.  */
  3872.       if (VF_DERIVED_VALUE (vfields))
  3873.         TREE_ADDRESSABLE (vfields) = 1;
  3874.       vfields = TREE_CHAIN (vfields);
  3875.     }
  3876.       if (any_default_members != 0)
  3877.     build_class_init_list (t);
  3878.     }
  3879.   else if (TYPE_NEEDS_CONSTRUCTING (t))
  3880.     build_class_init_list (t);
  3881.  
  3882.   if (! CLASSTYPE_DECLARED_EXCEPTION (t) && ! IS_SIGNATURE (t))
  3883.     embrace_waiting_friends (t);
  3884.  
  3885.   /* Write out inline function definitions.  */
  3886.   do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
  3887.   CLASSTYPE_INLINE_FRIENDS (t) = 0;
  3888.  
  3889.   if (CLASSTYPE_VSIZE (t) != 0)
  3890.     {
  3891.       if ((flag_this_is_variable & 1) == 0)
  3892.     {
  3893.       tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
  3894.                       TREE_TYPE (vfield));
  3895.       DECL_REGISTER (vtbl_ptr) = 1;
  3896.       CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
  3897.     }
  3898. #if 0
  3899.       /* This is now done above. */
  3900.       if (DECL_FIELD_CONTEXT (vfield) != t)
  3901.     {
  3902.       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
  3903.       tree offset = BINFO_OFFSET (binfo);
  3904.  
  3905.       vfield = copy_node (vfield);
  3906.       copy_lang_decl (vfield);
  3907.  
  3908.       if (! integer_zerop (offset))
  3909.         offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
  3910.       DECL_FIELD_CONTEXT (vfield) = t;
  3911.       DECL_CLASS_CONTEXT (vfield) = t;
  3912.       DECL_FIELD_BITPOS (vfield)
  3913.         = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
  3914.       CLASSTYPE_VFIELD (t) = vfield;
  3915.     }
  3916. #endif
  3917.  
  3918.       /* In addition to this one, all the other vfields should be listed. */
  3919.       /* Before that can be done, we have to have FIELD_DECLs for them, and
  3920.      a place to find them.  */
  3921.       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
  3922.  
  3923.       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
  3924.       && DECL_VINDEX (TREE_VEC_ELT (method_vec, 0)) == NULL_TREE)
  3925.     cp_warning ("`%#T' has virtual functions but non-virtual destructor",
  3926.             t);
  3927.     }
  3928.  
  3929.   /* Make the rtl for any new vtables we have created, and unmark
  3930.      the base types we marked.  */
  3931.   finish_vtbls (TYPE_BINFO (t), 1, t);
  3932.   TYPE_BEING_DEFINED (t) = 0;
  3933.  
  3934.   if (flag_dossier && CLASSTYPE_VTABLE_NEEDS_WRITING (t))
  3935.     {
  3936.       tree variants;
  3937.       tree tdecl;
  3938.  
  3939.       /* Now instantiate its type descriptors.  */
  3940.       tdecl = TREE_OPERAND (build_t_desc (t, 1), 0);
  3941.       variants = TYPE_POINTER_TO (t);
  3942.       build_type_variant (variants, 1, 0);
  3943.       while (variants)
  3944.     {
  3945.       build_t_desc (variants, 1);
  3946.       variants = TYPE_NEXT_VARIANT (variants);
  3947.     }
  3948.       variants = build_reference_type (t);
  3949.       build_type_variant (variants, 1, 0);
  3950.       while (variants)
  3951.     {
  3952.       build_t_desc (variants, 1);
  3953.       variants = TYPE_NEXT_VARIANT (variants);
  3954.     }
  3955.       DECL_CONTEXT (tdecl) = t;
  3956.     }
  3957.   /* Still need to instantiate this C struct's type descriptor.  */
  3958.   else if (flag_dossier && ! CLASSTYPE_DOSSIER (t))
  3959.     build_t_desc (t, 1);
  3960.  
  3961. #if 0
  3962.   if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
  3963.     undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
  3964. #endif
  3965.   if (current_class_type)
  3966.     popclass (0);
  3967.   else
  3968.     error ("trying to finish struct, but kicked out due to previous parse errors.");
  3969.  
  3970.   hack_incomplete_structures (t);
  3971.  
  3972.   resume_momentary (old);
  3973.  
  3974.   if (flag_cadillac)
  3975.     cadillac_finish_struct (t);
  3976.  
  3977. #if 0
  3978.   /* This has to be done after we have sorted out what to do with
  3979.      the enclosing type.  */
  3980.   if (write_symbols != DWARF_DEBUG)
  3981.     {
  3982.       /* Be smarter about nested classes here.  If a type is nested,
  3983.      only output it if we would output the enclosing type.  */
  3984.       if (DECL_CONTEXT (TYPE_NAME (t))
  3985.       && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_NAME (t)))) == 't')
  3986.     DECL_IGNORED_P (TYPE_NAME (t)) = TREE_ASM_WRITTEN (TYPE_NAME (t));
  3987.     }
  3988. #endif
  3989.  
  3990.   if (write_symbols != DWARF_DEBUG)
  3991.     {
  3992.       /* If the type has methods, we want to think about cutting down
  3993.      the amount of symbol table stuff we output.  The value stored in
  3994.      the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
  3995.      For example, if a member function is seen and we decide to
  3996.      write out that member function, then we can change the value
  3997.      of the DECL_IGNORED_P slot, and the type will be output when
  3998.      that member function's debug info is written out.  */
  3999.       if (CLASSTYPE_METHOD_VEC (t))
  4000.     {
  4001.       extern tree pending_vtables;
  4002.  
  4003.       /* Don't output full info about any type
  4004.          which does not have its implementation defined here.  */
  4005.       if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
  4006.         TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t))
  4007.           = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
  4008.       else if (CLASSTYPE_INTERFACE_ONLY (t))
  4009.         TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
  4010.       else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
  4011.         /* Only a first approximation!  */
  4012.         TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
  4013.     }
  4014.       else if (CLASSTYPE_INTERFACE_ONLY (t))
  4015.     TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
  4016.     }
  4017.  
  4018.   /* Finish debugging output for this type.  */
  4019.   rest_of_type_compilation (t, global_bindings_p ());
  4020.  
  4021.   return t;
  4022. }
  4023.  
  4024. /* Return non-zero if the effective type of INSTANCE is static.
  4025.    Used to determine whether the virtual function table is needed
  4026.    or not.
  4027.  
  4028.    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
  4029.    of our knowledge of its type.  */
  4030. int
  4031. resolves_to_fixed_type_p (instance, nonnull)
  4032.      tree instance;
  4033.      int *nonnull;
  4034. {
  4035.   switch (TREE_CODE (instance))
  4036.     {
  4037.     case INDIRECT_REF:
  4038.       /* Check that we are not going through a cast of some sort.  */
  4039.       if (TREE_TYPE (instance)
  4040.       == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
  4041.     instance = TREE_OPERAND (instance, 0);
  4042.       /* fall through...  */
  4043.     case CALL_EXPR:
  4044.       /* This is a call to a constructor, hence it's never zero.  */
  4045.       if (TREE_HAS_CONSTRUCTOR (instance))
  4046.     {
  4047.       if (nonnull)
  4048.         *nonnull = 1;
  4049.       return 1;
  4050.     }
  4051.       return 0;
  4052.  
  4053.     case SAVE_EXPR:
  4054.       /* This is a call to a constructor, hence it's never zero.  */
  4055.       if (TREE_HAS_CONSTRUCTOR (instance))
  4056.     {
  4057.       if (nonnull)
  4058.         *nonnull = 1;
  4059.       return 1;
  4060.     }
  4061.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4062.  
  4063.     case RTL_EXPR:
  4064.       /* This is a call to `new', hence it's never zero.  */
  4065.       if (TREE_CALLS_NEW (instance))
  4066.     {
  4067.       if (nonnull)
  4068.         *nonnull = 1;
  4069.       return 1;
  4070.     }
  4071.       return 0;
  4072.  
  4073.     case PLUS_EXPR:
  4074.     case MINUS_EXPR:
  4075.       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
  4076.     /* Propagate nonnull.  */
  4077.     resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4078.       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
  4079.     return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4080.       return 0;
  4081.  
  4082.     case NOP_EXPR:
  4083.     case CONVERT_EXPR:
  4084.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4085.  
  4086.     case ADDR_EXPR:
  4087.       if (nonnull)
  4088.     *nonnull = 1;
  4089.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4090.  
  4091.     case COMPONENT_REF:
  4092.       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
  4093.  
  4094.     case WITH_CLEANUP_EXPR:
  4095.       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
  4096.     return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
  4097.       /* fall through... */
  4098.     case VAR_DECL:
  4099.     case FIELD_DECL:
  4100.       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
  4101.       && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
  4102.     {
  4103.       if (nonnull)
  4104.         *nonnull = 1;
  4105.       return 1;
  4106.     }
  4107.       /* fall through... */
  4108.     case TARGET_EXPR:
  4109.     case PARM_DECL:
  4110.       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  4111.     {
  4112.       if (nonnull)
  4113.         *nonnull = 1;
  4114.       return 1;
  4115.     }
  4116.       else if (nonnull)
  4117.     {
  4118.       if (instance == current_class_decl
  4119.           && flag_this_is_variable <= 0)
  4120.         {
  4121.           /* Some people still use `this = 0' inside destructors.  */
  4122.           *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
  4123.           /* In a constructor, we know our type.  */
  4124.           if (flag_this_is_variable < 0)
  4125.         return 1;
  4126.         }
  4127.       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
  4128.         /* Reference variables should be references to objects.  */
  4129.         *nonnull = 1;
  4130.     }
  4131.       return 0;
  4132.  
  4133.     default:
  4134.       return 0;
  4135.     }
  4136. }
  4137.  
  4138. void
  4139. init_class_processing ()
  4140. {
  4141.   current_class_depth = 0;
  4142.   current_class_stacksize = 10;
  4143.   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
  4144.   current_class_stack = current_class_base;
  4145.  
  4146.   current_lang_stacksize = 10;
  4147.   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
  4148.   current_lang_stack = current_lang_base;
  4149.  
  4150.   /* Keep these values lying around.  */
  4151.   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
  4152.   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
  4153.   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
  4154.  
  4155.   gcc_obstack_init (&class_obstack);
  4156. }
  4157.  
  4158. /* Set current scope to NAME. CODE tells us if this is a
  4159.    STRUCT, UNION, or ENUM environment.
  4160.  
  4161.    NAME may end up being NULL_TREE if this is an anonymous or
  4162.    late-bound struct (as in "struct { ... } foo;")  */
  4163.  
  4164. /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
  4165.    appropriate values, found by looking up the type definition of
  4166.    NAME (as a CODE).
  4167.  
  4168.    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
  4169.    which can be seen locally to the class.  They are shadowed by
  4170.    any subsequent local declaration (including parameter names).
  4171.  
  4172.    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
  4173.    which have static meaning (i.e., static members, static
  4174.    member functions, enum declarations, etc).
  4175.  
  4176.    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
  4177.    which can be seen locally to the class (as in 1), but
  4178.    know that we are doing this for declaration purposes
  4179.    (i.e. friend foo::bar (int)).
  4180.  
  4181.    So that we may avoid calls to lookup_name, we cache the _TYPE
  4182.    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
  4183.  
  4184.    For multiple inheritance, we perform a two-pass depth-first search
  4185.    of the type lattice.  The first pass performs a pre-order search,
  4186.    marking types after the type has had its fields installed in
  4187.    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
  4188.    unmarks the marked types.  If a field or member function name
  4189.    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
  4190.    that name becomes `error_mark_node'.  */
  4191.  
  4192. void
  4193. pushclass (type, modify)
  4194.      tree type;
  4195.      int modify;
  4196. {
  4197.   push_memoized_context (type, modify);
  4198.  
  4199.   current_class_depth++;
  4200.   *current_class_stack++ = current_class_name;
  4201.   *current_class_stack++ = current_class_type;
  4202.   if (current_class_stack >= current_class_base + current_class_stacksize)
  4203.     {
  4204.       current_class_base =
  4205.     (tree *)xrealloc (current_class_base,
  4206.               sizeof (tree) * (current_class_stacksize + 10));
  4207.       current_class_stack = current_class_base + current_class_stacksize;
  4208.       current_class_stacksize += 10;
  4209.     }
  4210.  
  4211.   current_class_name = TYPE_NAME (type);
  4212.   if (TREE_CODE (current_class_name) == TYPE_DECL)
  4213.     current_class_name = DECL_NAME (current_class_name);
  4214.   current_class_type = type;
  4215.  
  4216.   if (previous_class_type != NULL_TREE
  4217.       && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
  4218.       && current_class_depth == 1)
  4219.     {
  4220.       /* Forcibly remove any old class remnants.  */
  4221.       popclass (-1);
  4222.       previous_class_type = NULL_TREE;
  4223.     }
  4224.  
  4225.   pushlevel_class ();
  4226.  
  4227.   if (modify)
  4228.     {
  4229.       tree tags;
  4230.       tree this_fndecl = current_function_decl;
  4231.  
  4232.       if (current_function_decl
  4233.       && DECL_CONTEXT (current_function_decl)
  4234.       && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
  4235.     current_function_decl = DECL_CONTEXT (current_function_decl);
  4236.       else
  4237.     current_function_decl = NULL_TREE;
  4238.  
  4239.       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
  4240.     declare_uninstantiated_type_level ();
  4241.       else if (type != previous_class_type || current_class_depth > 1)
  4242.     {
  4243.       build_mi_matrix (type);
  4244.       push_class_decls (type);
  4245.       free_mi_matrix ();
  4246.       if (current_class_depth == 1)
  4247.         previous_class_type = type;
  4248.     }
  4249.       else
  4250.     {
  4251.       tree item;
  4252.  
  4253.       /* Hooray, our cacheing was successful, let's just install the
  4254.          cached class_shadowed list, and walk through it to get the
  4255.          IDENTIFIER_TYPE_VALUEs correct.  */
  4256.       set_class_shadows (previous_class_values);
  4257.       for (item = previous_class_values; item; item = TREE_CHAIN (item))
  4258.         {
  4259.           tree id = TREE_PURPOSE (item);
  4260.           tree decl = IDENTIFIER_CLASS_VALUE (id);
  4261.  
  4262.           if (TREE_CODE (decl) == TYPE_DECL)
  4263.         set_identifier_type_value (id, TREE_TYPE (decl));
  4264.         }
  4265.       unuse_fields (type);
  4266.     }
  4267.  
  4268.       if (IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (type)))
  4269.     overload_template_name (current_class_name, 0);
  4270.  
  4271.       for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
  4272.     {
  4273.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
  4274.       if (! TREE_PURPOSE (tags))
  4275.         continue;
  4276.       pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0);
  4277.     }
  4278.  
  4279.       current_function_decl = this_fndecl;
  4280.     }
  4281.  
  4282.   if (flag_cadillac)
  4283.     cadillac_push_class (type);
  4284. }
  4285.  
  4286. /* Get out of the current class scope. If we were in a class scope
  4287.    previously, that is the one popped to.  The flag MODIFY tells whether
  4288.    the current scope declarations needs to be modified as a result of
  4289.    popping to the previous scope.  0 is used for class definitions.  */
  4290. void
  4291. popclass (modify)
  4292.      int modify;
  4293. {
  4294.   if (flag_cadillac)
  4295.     cadillac_pop_class ();
  4296.  
  4297.   if (modify < 0)
  4298.     {
  4299.       /* Back this old class out completely.  */
  4300.       tree tags = CLASSTYPE_TAGS (previous_class_type);
  4301.       tree t;
  4302.  
  4303.       /* This code can be seen as a cache miss.  When we've cached a
  4304.      class' scope's bindings and we can't use them, we need to reset
  4305.      them.  This is it!  */
  4306.       for (t = previous_class_values; t; t = TREE_CHAIN (t))
  4307.     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
  4308.       while (tags)
  4309.     {
  4310.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
  4311.       tags = TREE_CHAIN (tags);
  4312.     }
  4313.       goto ret;
  4314.     }
  4315.  
  4316.   if (modify)
  4317.     {
  4318.       /* Just remove from this class what didn't make
  4319.      it into IDENTIFIER_CLASS_VALUE.  */
  4320.       tree tags = CLASSTYPE_TAGS (current_class_type);
  4321.  
  4322.       while (tags)
  4323.     {
  4324.       TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
  4325.       tags = TREE_CHAIN (tags);
  4326.     }
  4327.       if (IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)))
  4328.     undo_template_name_overload (current_class_name, 0);
  4329.     }
  4330.  
  4331.   /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
  4332.      since not all class decls make it there currently.  */
  4333.   poplevel_class (! modify);
  4334.  
  4335.   /* Since poplevel_class does the popping of class decls nowadays,
  4336.      this really only frees the obstack used for these decls.
  4337.      That's why it had to be moved down here.  */
  4338.   if (modify)
  4339.     pop_class_decls (current_class_type);
  4340.  
  4341.   current_class_depth--;
  4342.   current_class_type = *--current_class_stack;
  4343.   current_class_name = *--current_class_stack;
  4344.  
  4345.   if (current_class_type)
  4346.     {
  4347.       if (CLASSTYPE_VTBL_PTR (current_class_type))
  4348.     {
  4349.       current_vtable_decl
  4350.         = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)),
  4351.                0);
  4352.       if (current_vtable_decl)
  4353.         current_vtable_decl = build_indirect_ref (current_vtable_decl,
  4354.                               NULL_PTR);
  4355.     }
  4356.       current_class_decl = lookup_name (this_identifier, 0);
  4357.       if (current_class_decl)
  4358.     {
  4359.       if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
  4360.         {
  4361.           tree temp;
  4362.           /* Can't call build_indirect_ref here, because it has special
  4363.          logic to return C_C_D given this argument.  */
  4364.           C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
  4365.           temp = TREE_TYPE (TREE_TYPE (current_class_decl));
  4366.           TREE_READONLY (C_C_D) = TYPE_READONLY (temp);
  4367.           TREE_SIDE_EFFECTS (C_C_D) = TYPE_VOLATILE (temp);
  4368.           TREE_THIS_VOLATILE (C_C_D) = TYPE_VOLATILE (temp);
  4369.         }
  4370.       else
  4371.         C_C_D = current_class_decl;
  4372.     }
  4373.       else
  4374.     C_C_D = NULL_TREE;
  4375.     }
  4376.   else
  4377.     {
  4378.       current_class_decl = NULL_TREE;
  4379.       current_vtable_decl = NULL_TREE;
  4380.       C_C_D = NULL_TREE;
  4381.     }
  4382.  
  4383.   pop_memoized_context (modify);
  4384.  
  4385.  ret:
  4386.   ;
  4387. }
  4388.  
  4389. /* When entering a class scope, all enclosing class scopes' names with
  4390.    static meaning (static variables, static functions, types and enumerators)
  4391.    have to be visible.  This recursive function calls pushclass for all
  4392.    enclosing class contexts until global or a local scope is reached.
  4393.    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
  4394.    formal of the same name.  */
  4395.  
  4396. void
  4397. push_nested_class (type, modify)
  4398.      tree type;
  4399.      int modify;
  4400. {
  4401.   tree context;
  4402.  
  4403.   if (type == error_mark_node || ! IS_AGGR_TYPE (type))
  4404.     return;
  4405.   
  4406.   context = DECL_CONTEXT (TYPE_NAME (type));
  4407.  
  4408.   if (context && TREE_CODE (context) == RECORD_TYPE)
  4409.     push_nested_class (context, 2);
  4410.   pushclass (type, modify);
  4411. }
  4412.  
  4413. /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
  4414.  
  4415. void
  4416. pop_nested_class (modify)
  4417.      int modify;
  4418. {
  4419.   tree context = DECL_CONTEXT (TYPE_NAME (current_class_type));
  4420.  
  4421.   popclass (modify);
  4422.   if (context && TREE_CODE (context) == RECORD_TYPE)
  4423.     pop_nested_class (modify);
  4424. }
  4425.  
  4426. /* Set global variables CURRENT_LANG_NAME to appropriate value
  4427.    so that behavior of name-mangling machinery is correct.  */
  4428.  
  4429. void
  4430. push_lang_context (name)
  4431.      tree name;
  4432. {
  4433.   *current_lang_stack++ = current_lang_name;
  4434.   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
  4435.     {
  4436.       current_lang_base =
  4437.     (tree *)xrealloc (current_lang_base,
  4438.               sizeof (tree) * (current_lang_stacksize + 10));
  4439.       current_lang_stack = current_lang_base + current_lang_stacksize;
  4440.       current_lang_stacksize += 10;
  4441.     }
  4442.  
  4443.   if (name == lang_name_cplusplus)
  4444.     {
  4445.       strict_prototype = strict_prototypes_lang_cplusplus;
  4446.       current_lang_name = name;
  4447.     }
  4448.   else if (name == lang_name_c)
  4449.     {
  4450.       strict_prototype = strict_prototypes_lang_c;
  4451.       current_lang_name = name;
  4452.     }
  4453.   else
  4454.     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
  4455.  
  4456.   if (flag_cadillac)
  4457.     cadillac_push_lang (name);
  4458. }
  4459.   
  4460. /* Get out of the current language scope.  */
  4461. void
  4462. pop_lang_context ()
  4463. {
  4464.   if (flag_cadillac)
  4465.     cadillac_pop_lang ();
  4466.  
  4467.   current_lang_name = *--current_lang_stack;
  4468.   if (current_lang_name == lang_name_cplusplus)
  4469.     strict_prototype = strict_prototypes_lang_cplusplus;
  4470.   else if (current_lang_name == lang_name_c)
  4471.     strict_prototype = strict_prototypes_lang_c;
  4472. }
  4473.  
  4474. int
  4475. root_lang_context_p ()
  4476. {
  4477.   return current_lang_stack == current_lang_base;
  4478. }
  4479.  
  4480. /* Type instantiation routines.  */
  4481.  
  4482. /* This function will instantiate the type of the expression given
  4483.    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
  4484.    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
  4485.  
  4486.    This function is used in build_modify_expr, convert_arguments,
  4487.    build_c_cast, and compute_conversion_costs.  */
  4488. tree
  4489. instantiate_type (lhstype, rhs, complain)
  4490.      tree lhstype, rhs;
  4491.      int complain;
  4492. {
  4493.   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
  4494.     {
  4495.       if (complain)
  4496.     error ("not enough type information");
  4497.       return error_mark_node;
  4498.     }
  4499.  
  4500.   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
  4501.     return rhs;
  4502.  
  4503.   /* This should really only be used when attempting to distinguish
  4504.      what sort of a pointer to function we have.  For now, any
  4505.      arithmetic operation which is not supported on pointers
  4506.      is rejected as an error.  */
  4507.  
  4508.   switch (TREE_CODE (rhs))
  4509.     {
  4510.     case TYPE_EXPR:
  4511.     case CONVERT_EXPR:
  4512.     case SAVE_EXPR:
  4513.     case CONSTRUCTOR:
  4514.     case BUFFER_REF:
  4515.       my_friendly_abort (177);
  4516.       return error_mark_node;
  4517.  
  4518.     case INDIRECT_REF:
  4519.     case ARRAY_REF:
  4520.       TREE_TYPE (rhs) = lhstype;
  4521.       lhstype = build_pointer_type (lhstype);
  4522.       TREE_OPERAND (rhs, 0)
  4523.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4524.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4525.     return error_mark_node;
  4526.  
  4527.       return rhs;
  4528.  
  4529.     case NOP_EXPR:
  4530.       rhs = copy_node (TREE_OPERAND (rhs, 0));
  4531.       TREE_TYPE (rhs) = unknown_type_node;
  4532.       return instantiate_type (lhstype, rhs, complain);
  4533.  
  4534.     case COMPONENT_REF:
  4535.       {
  4536.     tree field = TREE_OPERAND (rhs, 1);
  4537.     if (TREE_CODE (field) == TREE_LIST)
  4538.       {
  4539.         tree function = instantiate_type (lhstype, field, complain);
  4540.         if (function == error_mark_node)
  4541.           return error_mark_node;
  4542.         my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
  4543.         if (DECL_VINDEX (function))
  4544.           {
  4545.         tree base = TREE_OPERAND (rhs, 0);
  4546.         tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
  4547.         if (base_ptr == error_mark_node)
  4548.           return error_mark_node;
  4549.         base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
  4550.         if (base_ptr == error_mark_node)
  4551.           return error_mark_node;
  4552.         return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
  4553.           }
  4554.         return function;
  4555.       }
  4556.  
  4557.     my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
  4558.     my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
  4559.                   || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
  4560.                 179);
  4561.  
  4562.     TREE_TYPE (rhs) = lhstype;
  4563.     /* First look for an exact match  */
  4564.  
  4565.     while (field && TREE_TYPE (field) != lhstype)
  4566.       field = TREE_CHAIN (field);
  4567.     if (field)
  4568.       {
  4569.         TREE_OPERAND (rhs, 1) = field;
  4570.         return rhs;
  4571.       }
  4572.  
  4573.     /* No exact match found, look for a compatible function.  */
  4574.     field = TREE_OPERAND (rhs, 1);
  4575.     while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  4576.       field = TREE_CHAIN (field);
  4577.     if (field)
  4578.       {
  4579.         TREE_OPERAND (rhs, 1) = field;
  4580.         field = TREE_CHAIN (field);
  4581.         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
  4582.           field = TREE_CHAIN (field);
  4583.         if (field)
  4584.           {
  4585.         if (complain)
  4586.           error ("ambiguous overload for COMPONENT_REF requested");
  4587.         return error_mark_node;
  4588.           }
  4589.       }
  4590.     else
  4591.       {
  4592.         if (complain)
  4593.           error ("no appropriate overload exists for COMPONENT_REF");
  4594.         return error_mark_node;
  4595.       }
  4596.     return rhs;
  4597.       }
  4598.  
  4599.     case TREE_LIST:
  4600.       {
  4601.     tree elem, baselink, name;
  4602.     int globals = overloaded_globals_p (rhs);
  4603.  
  4604. #if 0 /* obsolete */
  4605.     /* If there's only one function we know about, return that.  */
  4606.     if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
  4607.       return TREE_VALUE (rhs);
  4608. #endif
  4609.  
  4610.     /* First look for an exact match.  Search either overloaded
  4611.        functions or member functions.  May have to undo what
  4612.        `default_conversion' might do to lhstype.  */
  4613.  
  4614.     if (TREE_CODE (lhstype) == POINTER_TYPE)
  4615.       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
  4616.           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
  4617.         lhstype = TREE_TYPE (lhstype);
  4618.       else
  4619.         {
  4620.           if (complain)
  4621.         error ("invalid type combination for overload");
  4622.           return error_mark_node;
  4623.         }
  4624.  
  4625.     if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
  4626.       {
  4627.         if (complain)
  4628.           cp_error ("cannot resolve overloaded function `%D' based on non-function type",
  4629.              TREE_PURPOSE (rhs));
  4630.         return error_mark_node;
  4631.       }
  4632.  
  4633.     if (globals > 0)
  4634.       {
  4635.         elem = get_first_fn (rhs);
  4636.         while (elem)
  4637.           if (TREE_TYPE (elem) != lhstype)
  4638.         elem = DECL_CHAIN (elem);
  4639.           else
  4640.         return elem;
  4641.         /* No exact match found, look for a compatible function.  */
  4642.         elem = get_first_fn (rhs);
  4643.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  4644.           elem = DECL_CHAIN (elem);
  4645.         if (elem)
  4646.           {
  4647.         tree save_elem = elem;
  4648.         elem = DECL_CHAIN (elem);
  4649.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem),
  4650.                             0))
  4651.           elem = DECL_CHAIN (elem);
  4652.         if (elem)
  4653.           {
  4654.             if (complain)
  4655.               {
  4656.             cp_error ("cannot resolve overload to target type `%#T'",
  4657.                   lhstype);
  4658.             cp_error_at ("  ambiguity between `%#D'", save_elem);
  4659.             cp_error_at ("  and `%#D', at least", elem);
  4660.               }
  4661.             return error_mark_node;
  4662.           }
  4663.         if (TREE_CODE (save_elem) == TEMPLATE_DECL)
  4664.           {
  4665.             int ntparms = TREE_VEC_LENGTH
  4666.               (DECL_TEMPLATE_PARMS (save_elem));
  4667.             tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
  4668.             int i, dummy;
  4669.             i = type_unification
  4670.               (DECL_TEMPLATE_PARMS (save_elem), targs,
  4671.                TYPE_ARG_TYPES (TREE_TYPE (save_elem)),
  4672.                TYPE_ARG_TYPES (lhstype), &dummy, 0);
  4673.             save_elem = instantiate_template (save_elem, targs);
  4674.           }
  4675.         return save_elem;
  4676.           }
  4677.         if (complain)
  4678.           {
  4679.         cp_error ("cannot resolve overload to target type `%#T'",
  4680.               lhstype);
  4681.         cp_error ("  because no suitable overload of function `%D' exists",
  4682.               TREE_PURPOSE (rhs));
  4683.           }
  4684.         return error_mark_node;
  4685.       }
  4686.  
  4687.     if (TREE_NONLOCAL_FLAG (rhs))
  4688.       {
  4689.         /* Got to get it as a baselink.  */
  4690.         rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
  4691.                    TREE_PURPOSE (rhs), 0);
  4692.       }
  4693.     else
  4694.       {
  4695.         my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
  4696.         if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
  4697.           rhs = TREE_VALUE (rhs);
  4698.         my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
  4699.                 182);
  4700.       }
  4701.  
  4702.     for (baselink = rhs; baselink;
  4703.          baselink = next_baselink (baselink))
  4704.       {
  4705.         elem = TREE_VALUE (baselink);
  4706.         while (elem)
  4707.           if (comptypes (lhstype, TREE_TYPE (elem), 1))
  4708.         return elem;
  4709.           else
  4710.         elem = TREE_CHAIN (elem);
  4711.       }
  4712.  
  4713.     /* No exact match found, look for a compatible method.  */
  4714.     for (baselink = rhs; baselink;
  4715.          baselink = next_baselink (baselink))
  4716.       {
  4717.         elem = TREE_VALUE (baselink);
  4718.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
  4719.           elem = TREE_CHAIN (elem);
  4720.         if (elem)
  4721.           {
  4722.         tree save_elem = elem;
  4723.         elem = TREE_CHAIN (elem);
  4724.         while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
  4725.           elem = TREE_CHAIN (elem);
  4726.         if (elem)
  4727.           {
  4728.             if (complain)
  4729.               error ("ambiguous overload for overloaded method requested");
  4730.             return error_mark_node;
  4731.           }
  4732.         return save_elem;
  4733.           }
  4734.         name = DECL_NAME (TREE_VALUE (rhs));
  4735. #if 0
  4736.         if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
  4737.           {
  4738.         /* Try to instantiate from non-member functions.  */
  4739.         rhs = lookup_name_nonclass (name);
  4740.         if (rhs && TREE_CODE (rhs) == TREE_LIST)
  4741.           {
  4742.             /* This code seems to be missing a `return'.  */
  4743.             my_friendly_abort (4);
  4744.             instantiate_type (lhstype, rhs, complain);
  4745.           }
  4746.           }
  4747. #endif
  4748.       }
  4749.     if (complain)
  4750.       error ("no static member functions named `%s'",
  4751.          IDENTIFIER_POINTER (name));
  4752.     return error_mark_node;
  4753.       }
  4754.  
  4755.     case CALL_EXPR:
  4756.       /* This is too hard for now.  */
  4757.       my_friendly_abort (183);
  4758.       return error_mark_node;
  4759.  
  4760.     case PLUS_EXPR:
  4761.     case MINUS_EXPR:
  4762.     case COMPOUND_EXPR:
  4763.       TREE_OPERAND (rhs, 0)
  4764.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4765.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4766.     return error_mark_node;
  4767.       TREE_OPERAND (rhs, 1)
  4768.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4769.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4770.     return error_mark_node;
  4771.  
  4772.       TREE_TYPE (rhs) = lhstype;
  4773.       return rhs;
  4774.  
  4775.     case MULT_EXPR:
  4776.     case TRUNC_DIV_EXPR:
  4777.     case FLOOR_DIV_EXPR:
  4778.     case CEIL_DIV_EXPR:
  4779.     case ROUND_DIV_EXPR:
  4780.     case RDIV_EXPR:
  4781.     case TRUNC_MOD_EXPR:
  4782.     case FLOOR_MOD_EXPR:
  4783.     case CEIL_MOD_EXPR:
  4784.     case ROUND_MOD_EXPR:
  4785.     case FIX_ROUND_EXPR:
  4786.     case FIX_FLOOR_EXPR:
  4787.     case FIX_CEIL_EXPR:
  4788.     case FIX_TRUNC_EXPR:
  4789.     case FLOAT_EXPR:
  4790.     case NEGATE_EXPR:
  4791.     case ABS_EXPR:
  4792.     case MAX_EXPR:
  4793.     case MIN_EXPR:
  4794.     case FFS_EXPR:
  4795.  
  4796.     case BIT_AND_EXPR:
  4797.     case BIT_IOR_EXPR:
  4798.     case BIT_XOR_EXPR:
  4799.     case LSHIFT_EXPR:
  4800.     case RSHIFT_EXPR:
  4801.     case LROTATE_EXPR:
  4802.     case RROTATE_EXPR:
  4803.  
  4804.     case PREINCREMENT_EXPR:
  4805.     case PREDECREMENT_EXPR:
  4806.     case POSTINCREMENT_EXPR:
  4807.     case POSTDECREMENT_EXPR:
  4808.       if (complain)
  4809.     error ("illegal operation on uninstantiated type");
  4810.       return error_mark_node;
  4811.  
  4812.     case TRUTH_AND_EXPR:
  4813.     case TRUTH_OR_EXPR:
  4814.     case TRUTH_XOR_EXPR:
  4815.     case LT_EXPR:
  4816.     case LE_EXPR:
  4817.     case GT_EXPR:
  4818.     case GE_EXPR:
  4819.     case EQ_EXPR:
  4820.     case NE_EXPR:
  4821.     case TRUTH_ANDIF_EXPR:
  4822.     case TRUTH_ORIF_EXPR:
  4823.     case TRUTH_NOT_EXPR:
  4824.       if (complain)
  4825.     error ("not enough type information");
  4826.       return error_mark_node;
  4827.  
  4828.     case COND_EXPR:
  4829.       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
  4830.     {
  4831.       if (complain)
  4832.         error ("not enough type information");
  4833.       return error_mark_node;
  4834.     }
  4835.       TREE_OPERAND (rhs, 1)
  4836.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4837.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4838.     return error_mark_node;
  4839.       TREE_OPERAND (rhs, 2)
  4840.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
  4841.       if (TREE_OPERAND (rhs, 2) == error_mark_node)
  4842.     return error_mark_node;
  4843.  
  4844.       TREE_TYPE (rhs) = lhstype;
  4845.       return rhs;
  4846.  
  4847.     case MODIFY_EXPR:
  4848.       TREE_OPERAND (rhs, 1)
  4849.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
  4850.       if (TREE_OPERAND (rhs, 1) == error_mark_node)
  4851.     return error_mark_node;
  4852.  
  4853.       TREE_TYPE (rhs) = lhstype;
  4854.       return rhs;
  4855.       
  4856.     case ADDR_EXPR:
  4857.       if (TYPE_PTRMEMFUNC_P (lhstype))
  4858.     lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
  4859.       else if (TREE_CODE (lhstype) != POINTER_TYPE)
  4860.     {
  4861.       if (complain)
  4862.         error ("type for resolving address of overloaded function must be pointer type");
  4863.       return error_mark_node;
  4864.     }
  4865.       TREE_TYPE (rhs) = lhstype;
  4866.       lhstype = TREE_TYPE (lhstype);
  4867.       TREE_OPERAND (rhs, 0)
  4868.     = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
  4869.       if (TREE_OPERAND (rhs, 0) == error_mark_node)
  4870.     return error_mark_node;
  4871.  
  4872.       mark_addressable (TREE_OPERAND (rhs, 0));
  4873.       return rhs;
  4874.  
  4875.     case ENTRY_VALUE_EXPR:
  4876.       my_friendly_abort (184);
  4877.       return error_mark_node;
  4878.  
  4879.     case ERROR_MARK:
  4880.       return error_mark_node;
  4881.  
  4882.     default:
  4883.       my_friendly_abort (185);
  4884.       return error_mark_node;
  4885.     }
  4886. }
  4887.  
  4888. /* Return the name of the virtual function pointer field
  4889.    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
  4890.    this may have to look back through base types to find the
  4891.    ultimate field name.  (For single inheritance, these could
  4892.    all be the same name.  Who knows for multiple inheritance).  */
  4893. static tree
  4894. get_vfield_name (type)
  4895.      tree type;
  4896. {
  4897.   tree binfo = TYPE_BINFO (type);
  4898.   char *buf;
  4899.  
  4900.   while (BINFO_BASETYPES (binfo)
  4901.      && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
  4902.      && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
  4903.     binfo = BINFO_BASETYPE (binfo, 0);
  4904.  
  4905.   type = BINFO_TYPE (binfo);
  4906.   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
  4907.             + TYPE_NAME_LENGTH (type) + 2);
  4908.   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
  4909.   return get_identifier (buf);
  4910. }
  4911.  
  4912. void
  4913. print_class_statistics ()
  4914. {
  4915. #ifdef GATHER_STATISTICS
  4916.   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
  4917.   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
  4918.   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
  4919.        n_build_method_call, n_inner_fields_searched);
  4920.   if (n_vtables)
  4921.     {
  4922.       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
  4923.            n_vtables, n_vtable_searches);
  4924.       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
  4925.            n_vtable_entries, n_vtable_elems);
  4926.     }
  4927. #endif
  4928. }
  4929.  
  4930. /* Push an obstack which is sufficiently long-lived to hold such class
  4931.    decls that may be cached in the previous_class_values list.  For now, let's
  4932.    use the permanent obstack, later we may create a dedicated obstack just
  4933.    for this purpose.  The effect is undone by pop_obstacks.  */
  4934. void
  4935. maybe_push_cache_obstack ()
  4936. {
  4937.   push_obstacks_nochange ();
  4938.   if (current_class_depth == 1)
  4939.     current_obstack = &permanent_obstack;
  4940. }
  4941.